def __init__(self, *args, **kwargs):
     DummyCache.__init__(self, *args, **kwargs)
     self.setex = None
     self.lrem = None
     self.zadd = None
     self.pipeline = None
     self.ttl = None
Esempio n. 2
0
 def init(self, name_encode):
     cache_conf = django_settings.CACHES['mdir-cache']
     cache_location = os.path.join(cache_conf.get('LOCATION'),
                                   str(self.macct.get('id')),
                                   name_encode.decode())
     #~ self.log.dbg('cache_conf: ', cache_conf)
     self.log.dbg('cache_location: ', cache_location)
     if self.conf.get('MDIR_CACHE_ENABLE', False):
         self.__cache = FileBasedCache(cache_location, cache_conf)
     else:
         self.__cache = DummyCache('localhost', cache_conf)
     self.log.dbg('mdir cache: ', self.__cache)
     self._meta_ttl = self.conf.get('MDIR_CACHE_META_TTL')
     self._data_ttl = self.conf.get('MDIR_CACHE_DATA_TTL')
Esempio n. 3
0
 def test_allows_other_apps_to_wrap_the_cache(self, django_caches):
     manual_cache = DummyCache('dummy', {})
     to_patch = 'django.core.cache.CacheHandler.__getitem__'
     with patch(to_patch, return_value=manual_cache) as mock:
         cfg = CacheConfig(model_admin=None)
         assert cache.caches['default'] == manual_cache
         assert cfg.cache == manual_cache
         assert mock.called
Esempio n. 4
0
def get_request_cache():
    """
    Returns per-request cache when running RequestCacheMiddleware otherwise a
    DummyCache instance (when running periodic tasks, tests or shell)
    """
    try:
        return _request_cache[currentThread()]
    except KeyError:
        return DummyCache('dummy', {})
Esempio n. 5
0
 def setUp(self):
     self.old_cache = cache.cache
     #Disable caching (to not interfere with production cache,
     #not sure if that's possible but let's not risk it)
     cache.cache = DummyCache('', {})
     if 'postgresql' in askbot.get_database_engine_name():
         management.call_command('init_postgresql_full_text_search',
                                 verbosity=0,
                                 interactive=False)
Esempio n. 6
0
    def test_thread_summary_rendering_dummy_cache(self):
        cache.cache = DummyCache('', {})  # Disable caching

        ss = SearchState.get_empty()
        thread = self.q.thread
        test_html = thread.get_summary_html(search_state=ss)

        context = {
            'thread': thread,
            'question': thread._question_post(),
            'search_state': ss,
            'visitor': None
        }
        proper_html = get_template('widgets/question_summary.html').render(
            context)
        self.assertEqual(test_html, proper_html)

        # Make double-check that all tags are included
        self.assertTrue(ss.add_tag('tag1').full_url() in test_html)
        self.assertTrue(ss.add_tag('tag2').full_url() in test_html)
        self.assertTrue(ss.add_tag('tag3').full_url() in test_html)
        self.assertFalse(ss.add_tag('mini-mini').full_url() in test_html)

        # Make sure that title and body text are escaped properly.
        # This should be obvious at this point, if the above test passes, but why not be explicit
        # UPDATE: And voila, these tests catched double-escaping bug in template, where `<` was `<`
        #         And indeed, post.summary is escaped before saving, in parse_and_save()
        # UPDATE 2:Weird things happen with question summary (it's double escaped etc., really weird) so
        # let's just make sure that there are no tag placeholders left
        self.assertTrue(
            '<<<tag1>>> fake title' in proper_html)
        #self.assertTrue('<<<tag2>>> <<<tag3>>> cheating' in proper_html)
        self.assertFalse('<<<tag1>>>' in proper_html)
        self.assertFalse('<<<tag2>>>' in proper_html)
        self.assertFalse('<<<tag3>>>' in proper_html)

        ###

        ss = ss.add_tag('mini-mini')
        context['search_state'] = ss
        test_html = thread.get_summary_html(search_state=ss)
        proper_html = get_template('widgets/question_summary.html').render(
            context)

        self.assertEqual(test_html, proper_html)

        # Make double-check that all tags are included (along with `mini-mini` tag)
        self.assertTrue(ss.add_tag('tag1').full_url() in test_html)
        self.assertTrue(ss.add_tag('tag2').full_url() in test_html)
        self.assertTrue(ss.add_tag('tag3').full_url() in test_html)
Esempio n. 7
0
def disable_django_caching(backend):
    """
    context manager that disables django caching by using a DummyCache in place of the current cache
    """

    old_cache = backend.cache

    # create new cache
    backend.cache = DummyCache("localhost", params={})

    try:
        yield
    finally:
        # enable old cache
        backend.cache = old_cache
Esempio n. 8
0
 def setUp(self):
     self.old_cache = cache.cache
     #Disable caching (to not interfere with production cache,
     #not sure if that's possible but let's not risk it)
     cache.cache = DummyCache('', {})
Esempio n. 9
0
 def setUp(self):
     # Monkey-patch in the dummy cache
     self.cache = cache
     cacheback.base.cache = DummyCache('unique-snowflake', {})
     self.job = SingleArgJob()
Esempio n. 10
0
from badges.models import BadgeInstance
from banners.tasks import add_click, old_add_click
from shared.tests import TestCase


def _get_clicks(instance_id):
    return (BadgeInstance.objects.get(pk=instance_id).clickstats_set.aggregate(
        Sum('clicks'))['clicks__sum'])


def _get_normalized_clicks(instance_id):
    return BadgeInstance.objects.get(pk=instance_id).clicks


@patch('caching.base.cache', DummyCache(None, {}))
class AddClickTests(TestCase):
    fixtures = ['banners']

    def test_basic(self):
        """Test that the task increments the correct clickstats object."""
        old_clicks = _get_clicks(2)
        add_click(2)
        new_clicks = _get_clicks(2)

        eq_(old_clicks + 1, new_clicks)

    def test_normalized(self):
        """Test that the task increments the normalized click count as well."""
        old_clicks = _get_normalized_clicks(2)
        add_click(2)
Esempio n. 11
0
 def __init__(self, *args, **kwargs):
     DummyCache.__init__(self, *args, **kwargs)
Esempio n. 12
0
class JMailMDirCache(JMailBase):
    __cache = None
    _meta_ttl = None
    _data_ttl = None

    @classmethod
    def init(self, name_encode):
        cache_conf = django_settings.CACHES['mdir-cache']
        cache_location = os.path.join(cache_conf.get('LOCATION'),
                                      str(self.macct.get('id')),
                                      name_encode.decode())
        #~ self.log.dbg('cache_conf: ', cache_conf)
        self.log.dbg('cache_location: ', cache_location)
        if self.conf.get('MDIR_CACHE_ENABLE', False):
            self.__cache = FileBasedCache(cache_location, cache_conf)
        else:
            self.__cache = DummyCache('localhost', cache_conf)
        self.log.dbg('mdir cache: ', self.__cache)
        self._meta_ttl = self.conf.get('MDIR_CACHE_META_TTL')
        self._data_ttl = self.conf.get('MDIR_CACHE_DATA_TTL')

    @classmethod
    def __msg_cache_key(self, dtype, msg_uid):
        return 'msg:{}:{}'.format(dtype, int(msg_uid))

    @classmethod
    def mdir_attribs_set(self, mdir_name, attribs):
        if type(mdir_name) is bytes:
            mdir_name = mdir_name.decode()
        self.__cache.set('mdir:attribs:' + mdir_name, attribs, self._meta_ttl)
        self.log.dbg('CACHE save: ', 'mdir:attribs:' + mdir_name)

    @classmethod
    def mdir_attribs_get(self, mdir_name):
        if type(mdir_name) is bytes:
            mdir_name = mdir_name.decode()
        return self.__cache.get('mdir:attribs:' + mdir_name, None)

    @classmethod
    def subs_list_get(self):
        return self.__cache.get('subs:list', None)

    @classmethod
    def subs_list_set(self, sl):
        self.__cache.set('subs:list', sl, self._meta_ttl)

    @classmethod
    def msg_data_get(self, msg_uid):
        ck = self.__msg_cache_key('data', msg_uid)
        return self.__cache.get(ck, None)

    @classmethod
    def msg_data_set(self, msg_uid, data):
        ck = self.__msg_cache_key('data', msg_uid)
        self.__cache.set(ck, data, self._data_ttl)
        self.log.dbg('CACHE save: msg data - %s' % msg_uid)

    @classmethod
    def msg_meta_get(self, msg_uid):
        ck = self.__msg_cache_key('meta', msg_uid)
        return self.__cache.get(ck, None)

    @classmethod
    def msg_meta_set(self, msg_uid, meta):
        ck = self.__msg_cache_key('meta', msg_uid)
        self.__cache.set(ck, meta, self._meta_ttl)
        self.log.dbg('CACHE save: msg meta - %s' % msg_uid)
Esempio n. 13
0
def init_dummy_cache():
    return DummyCache(host='localhost', params={})