<commit_before>try:
    import cPickle as pickle
except ImportError:
    import pickle
from threading import local

from django.core.cache.backends.memcached import BaseMemcachedCache

from . import client


def serialize_pickle(key, value):
    if isinstance(value, basestring):
        return value, 1
    return pickle.dumps(value), 2

def deserialize_pickle(key, value, flags):
    if flags == 1:
        return value
    if flags == 2:
        return pickle.loads(value)
    raise Exception('Unknown flags for value: {1}'.format(flags))


class PyMemcacheCache(BaseMemcachedCache):
    """An implementation of a cache binding using pymemcache."""

    def __init__(self, server, params):
        self._local = local()
        if isinstance(server, basestring):
            server = server.split(';')
        server = server[0]  # Only support one server for now.
        super(PyMemcacheCache, self).__init__(server, params,
                                              library=client,
                                              value_not_found_exception=ValueError)

    @property
    def _cache(self):
        client = getattr(self._local, 'client', None)
        if client:
            return client

        # pymemcached uses cache options as kwargs to the __init__ method.
        options = {
            'serializer': serialize_pickle,
            'deserializer': deserialize_pickle,
        }
        options.update(**self._options)
        host, port = self._servers[0].split(':')
        server = (host, int(port))
        self._local.client = self._lib.Client(server, **options)<commit_msg>Don’t use the options dict if it’s `None`.<commit_after>try:
    import cPickle as pickle
except ImportError:
    import pickle
from threading import local

from django.core.cache.backends.memcached import BaseMemcachedCache

from . import client


def serialize_pickle(key, value):
    if isinstance(value, basestring):
        return value, 1
    return pickle.dumps(value), 2

def deserialize_pickle(key, value, flags):
    if flags == 1:
        return value
    if flags == 2:
        return pickle.loads(value)
    raise Exception('Unknown flags for value: {1}'.format(flags))


class PyMemcacheCache(BaseMemcachedCache):
    """An implementation of a cache binding using pymemcache."""

    def __init__(self, server, params):
        self._local = local()
        if isinstance(server, basestring):
            server = server.split(';')
        server = server[0]  # Only support one server for now.
        super(PyMemcacheCache, self).__init__(server, params,
                                              library=client,
                                              value_not_found_exception=ValueError)

    @property
    def _cache(self):
        client = getattr(self._local, 'client', None)
        if client:
            return client

        # pymemcached uses cache options as kwargs to the __init__ method.
        options = {
            'serializer': serialize_pickle,
            'deserializer': deserialize_pickle,
        }
        if self._options:
            options.update(**self._options)
        host, port = self._servers[0].split(':')
        server = (host, int(port))
        self._local.client = self._lib.Client(server, **options)
{'fraction_non_alphanumeric': 0.09484598963098506, 'fraction_numerical': 0.004269594388533089, 'mean_word_length': 2.3198380566801617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '319807', 'n_tokens_mistral': 901, 'n_tokens_neox': 883, 'n_words': 252}