<commit_before><?php

namespace Brother\CommonBundle\Cache;

use Brother\CommonBundle\AppDebug;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Predis\ClientInterface;

class BrotherCacheProvider extends CacheProvider {

    /**
     * @var ClientInterface|\Predis\Client
     */
    private $client;

    /**
     * @param ClientInterface $client
     *
     * @return \Doctrine\Common\Cache\PredisCache
     */
    public function __construct(ClientInterface $client) {
        $this->client = $client;
    }

    /**
     * {@inheritdoc}
     */
    protected function doFetch($id) {
        $result = $this->client->get($id);
        if (null === $result) {
            return false;
        }

        return unserialize($result);
    }

    /**
     * {@inheritdoc}
     */
    protected function doFetchMultiple(array $keys) {
        $fetchedItems = call_user_func_array(array($this->client, 'mget'), $keys);

        return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
    }

    /**
     * {@inheritdoc}
     */
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) {
        if ($lifetime) {
            $success = true;

            // Keys have lifetime, use SETEX for each of them
            foreach ($keysAndValues as $key => $value) {
                $response = $this->client->setex($key, $lifetime, serialize($value));

                if ((string)$response != 'OK') {
                    $success = false;
                }
            }

            return $success;
        }

        // No lifetime, use MSET
        $response = $this->client->mset(array_map(function ($value) {
            return serialize($value);
        }, $keysAndValues));

        return (string)$response == 'OK';
    }

    /**
     * {@inheritdoc}
     */
    protected function doContains($id) {
        return $this->client->exists($id);
    }

    /**
     * {@inheritdoc}
     */
    protected function doSave($id, $data, $lifeTime = 0) {
//        AppDebug::_dx($id);
        if (is_object($data)) {
            switch (get_class($data)) {
                case 'Sol\NewsBundle\Document\News':
                case 'Sol\NewsBundle\Document\Tag':
                case 'Sol\NewsBundle\Document\Favorite':
                case 'Sol\NewsBundle\Document\RssSource':
                    break;
                default:
                    AppDebug::_dx([$id, get_class($data), $data]);
                    break;
            }
        } elseif (is_array($data)) {
//            AppDebug::_dx([$id, array_keys($data), $data]);
        }
        $data = serialize($data);
        if (strlen($data) > 100000) {
            AppDebug::_dx([$id, $data, $lifeTime]);
        }
        if ($lifeTime > 0) {
            $response = $this->client->setex($id, $lifeTime, $data);
        } else {
            $response = $this->client->set($id, $data);
        }<commit_msg>Cache<commit_after><?php

namespace Brother\CommonBundle\Cache;

use Brother\CommonBundle\AppDebug;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Predis\ClientInterface;

class BrotherCacheProvider extends CacheProvider {

    /**
     * @var ClientInterface|\Predis\Client
     */
    private $client;

    /**
     * @param ClientInterface $client
     *
     * @return \Doctrine\Common\Cache\PredisCache
     */
    public function __construct(ClientInterface $client) {
        $this->client = $client;
    }

    /**
     * {@inheritdoc}
     */
    protected function doFetch($id) {
        $result = $this->client->get($id);
        if (null === $result) {
            return false;
        }

        return unserialize($result);
    }

    /**
     * {@inheritdoc}
     */
    protected function doFetchMultiple(array $keys) {
        $fetchedItems = call_user_func_array(array($this->client, 'mget'), $keys);

        return array_map('unserialize', array_filter(array_combine($keys, $fetchedItems)));
    }

    /**
     * {@inheritdoc}
     */
    protected function doSaveMultiple(array $keysAndValues, $lifetime = 0) {
        if ($lifetime) {
            $success = true;

            // Keys have lifetime, use SETEX for each of them
            foreach ($keysAndValues as $key => $value) {
                $response = $this->client->setex($key, $lifetime, serialize($value));

                if ((string)$response != 'OK') {
                    $success = false;
                }
            }

            return $success;
        }

        // No lifetime, use MSET
        $response = $this->client->mset(array_map(function ($value) {
            return serialize($value);
        }, $keysAndValues));

        return (string)$response == 'OK';
    }

    /**
     * {@inheritdoc}
     */
    protected function doContains($id) {
        return $this->client->exists($id);
    }

    /**
     * {@inheritdoc}
     */
    protected function doSave($id, $data, $lifeTime = 0) {
//        AppDebug::_dx($id);
        if (is_object($data)) {
            switch (get_class($data)) {
                case 'Sol\NewsBundle\Document\News':
                case 'Sol\NewsBundle\Document\NewsBlock':
                case 'Sol\NewsBundle\Document\Tag':
                case 'Sol\NewsBundle\Document\Favorite':
                case 'Sol\NewsBundle\Document\RssSource':
                    break;
                default:
                    AppDebug::_dx([$id, get_class($data), $data]);
                    break;
            }
        } elseif (is_array($data)) {
//            AppDebug::_dx([$id, array_keys($data), $data]);
        }
        $data = serialize($data);
        if (strlen($data) > 100000) {
            AppDebug::_dx([$id, $data, $lifeTime]);
        }
        if ($lifeTime > 0) {
            $response = $this->client->setex($id, $lifeTime, $data);
        } else {
            $response = $this->client->set($id, $data);
        }
{'fraction_non_alphanumeric': 0.14690026954177898, 'fraction_numerical': 0.003032345013477089, 'mean_word_length': 1.848848368522073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '494928', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1679, 'n_words': 324}