<commit_before><?php

namespace Psr\Cache;

/**
 * Basic implementation of a backend-agnostic cache item.
 *
 * @implements \Psr\Cache\ItemInterface
 */
trait BasicCacheItemTrait {

    /**
     * @var string
     */
    protected $key;

    /**
     * @var mixed
     */
    protected $value;

    /**
     * @var boolean
     */
    protected $hit;

    /**
     * @var \DateTime
     */
    protected $expiration;

    /**
     * {@inheritdoc}
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * {@inheritdoc}
     */
    public function get()
    {
        return $this->isHit() ? $this->value : NULL;
    }

    /**
     * {@inheritdoc}
     */
    public function set($value, $ttl = null)
    {
        $this->value = $value;
        $this->setExpiration($ttl);
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function save($value = null, $ttl = null)
    {
        if ($value) {
            $this->set($value, $ttl);
        }
        $this->write($this->key, $this->value, $this->ttd);
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHit()
    {
        return $this->hit;
    }

    /**
     * {@inheritdoc}
     */
    public function delete()
    {
        $this->db->delete($this->key);
    }

    /**
     * {@inheritdoc}
     */
    public function exists()
    {
        return $this->hit;
    }

    /**
     * Sets the expiration for this cache item.
     *
     * @param mixed $ttl
     *   The TTL to convert to a DateTime expiration.
     */
    protected function setExpiration($ttl) {
        if ($ttl instanceof \DateTime) {
            $this->ttd = $ttl;
        }
        elseif (is_int($ttl)) {
            $this->ttd = new \DateTime('now +' . $ttl . ' seconds');
        }
        elseif (is_null($this->ttd)) {
            $this->ttd = new \DateTime('now +1 year');
        }
    }

    /**
     * Commits this cache item to storage.
     *
     * @param $key
     *   The key of the cache item to save.
     * @param $value
     *   The value to save. It should not be serialized.
     * @param \DateTime $expiration
     *   The timestamp after which this cache item should be considered expired.
     */
    protected abstract function write($key, $value, \DateTime $expiration);
}<commit_msg>BasicCacheItemTrait::save() should not assume that write was successful<commit_after><?php

namespace Psr\Cache;

/**
 * Basic implementation of a backend-agnostic cache item.
 *
 * @implements \Psr\Cache\ItemInterface
 */
trait BasicCacheItemTrait {

    /**
     * @var string
     */
    protected $key;

    /**
     * @var mixed
     */
    protected $value;

    /**
     * @var boolean
     */
    protected $hit;

    /**
     * @var \DateTime
     */
    protected $expiration;

    /**
     * {@inheritdoc}
     */
    public function getKey()
    {
        return $this->key;
    }

    /**
     * {@inheritdoc}
     */
    public function get()
    {
        return $this->isHit() ? $this->value : NULL;
    }

    /**
     * {@inheritdoc}
     */
    public function set($value, $ttl = null)
    {
        $this->value = $value;
        $this->setExpiration($ttl);
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function save($value = null, $ttl = null)
    {
        if ($value) {
            $this->set($value, $ttl);
        }
        return $this->write($this->key, $this->value, $this->ttd);
    }

    /**
     * {@inheritdoc}
     */
    public function isHit()
    {
        return $this->hit;
    }

    /**
     * {@inheritdoc}
     */
    public function delete()
    {
        $this->db->delete($this->key);
    }

    /**
     * {@inheritdoc}
     */
    public function exists()
    {
        return $this->hit;
    }

    /**
     * Sets the expiration for this cache item.
     *
     * @param mixed $ttl
     *   The TTL to convert to a DateTime expiration.
     */
    protected function setExpiration($ttl) {
        if ($ttl instanceof \DateTime) {
            $this->ttd = $ttl;
        }
        elseif (is_int($ttl)) {
            $this->ttd = new \DateTime('now +' . $ttl . ' seconds');
        }
        elseif (is_null($this->ttd)) {
            $this->ttd = new \DateTime('now +1 year');
        }
    }

    /**
     * Commits this cache item to storage.
     *
     * @param $key
     *   The key of the cache item to save.
     * @param $value
     *   The value to save. It should not be serialized.
     * @param \DateTime $expiration
     *   The timestamp after which this cache item should be considered expired.
     * @return boolean
     *   Returns true if the item was successfully committed, or false if there was
     *   an error.
     */
    protected abstract function write($key, $value, \DateTime $expiration);
}
{'fraction_non_alphanumeric': 0.14584195283409185, 'fraction_numerical': 0.0004137360364087712, 'mean_word_length': 1.6291462751495378, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3358143', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1494, 'n_words': 377}