def test_memcache_wrapper(self): try: from django.core.cache.backends import memcached from django.core.cache.backends import locmem except InvalidCacheBackendError: sys.stderr.write( "\n* Memcache library is not installed. Skipping test.\n") return prev_cache_cls = memcached.CacheClass memcached.CacheClass = locmem.CacheClass prev_backend_module = sys.modules.pop("djcelery.backends.cache") try: from djcelery.backends.cache import cache, DjangoMemcacheWrapper self.assertIsInstance(cache, DjangoMemcacheWrapper) key = "cu.test_memcache_wrapper" val = "The quick brown fox." default = "The lazy dog." self.assertEqual(cache.get(key, default=default), default) cache.set(key, val) self.assertEqual(pickle.loads(cache.get(key, default=default)), val) finally: memcached.CacheClass = prev_cache_cls sys.modules["djcelery.backends.cache"] = prev_backend_module
def test_memcache_wrapper(self): try: from django.core.cache.backends import memcached from django.core.cache.backends import locmem except InvalidCacheBackendError: sys.stderr.write( '\n* Memcache library is not installed. Skipping test.\n') return prev_cache_cls = memcached.CacheClass memcached.CacheClass = locmem.CacheClass prev_backend_module = sys.modules.pop('djcelery.backends.cache') try: from djcelery.backends.cache import cache key = 'cu.test_memcache_wrapper' val = 'The quick brown fox.' default = 'The lazy dog.' self.assertEqual(cache.get(key, default=default), default) cache.set(key, val) self.assertEqual(cache.get(key, default=default), val) finally: memcached.CacheClass = prev_cache_cls sys.modules['djcelery.backends.cache'] = prev_backend_module
def test_memcache_wrapper(self): try: from django.core.cache.backends import memcached from django.core.cache.backends import locmem except InvalidCacheBackendError: sys.stderr.write( '\n* Memcache library is not installed. Skipping test.\n') return try: prev_cache_cls = memcached.CacheClass memcached.CacheClass = locmem.CacheClass except AttributeError: return prev_backend_module = sys.modules.pop('djcelery.backends.cache') try: from djcelery.backends.cache import cache key = 'cu.test_memcache_wrapper' val = 'The quick brown fox.' default = 'The lazy dog.' self.assertEqual(cache.get(key, default=default), default) cache.set(key, val) self.assertEqual(cache.get(key, default=default), val) finally: memcached.CacheClass = prev_cache_cls sys.modules['djcelery.backends.cache'] = prev_backend_module
def test_memcache_wrapper(self): try: from django.core.cache.backends import memcached from django.core.cache.backends import locmem except InvalidCacheBackendError: sys.stderr.write( "\n* Memcache library is not installed. Skipping test.\n") return prev_cache_cls = memcached.CacheClass memcached.CacheClass = locmem.CacheClass prev_backend_module = sys.modules.pop("djcelery.backends.cache") try: from djcelery.backends.cache import cache, DjangoMemcacheWrapper self.assertIsInstance(cache, DjangoMemcacheWrapper) key = "cu.test_memcache_wrapper" val = "The quick brown fox." default = "The lazy dog." self.assertEqual(cache.get(key, default=default), default) cache.set(key, val) self.assertEqual(pickle.loads(cache.get(key, default=default)), val) finally: memcached.CacheClass = prev_cache_cls sys.modules["djcelery.backends.cache"] = prev_backend_module
def get(self, request): """显示首页""" context = cache.get('index_page_data') if context is None: types = GoodsType.objects.all() goods_banners = IndexGoodsBanner.objects.all().order_by('index') promotion_banners = IndexPromotionBanner.objects.all().order_by('index') for type in types: type.title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index') type.image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index') context = {'types': types, 'goods_banners': goods_banners, 'promotion_banners': promotion_banners} cache.set('index_page_data', context, 3600) user = request.user cart_count = 0 if user.is_authenticated(): conn = get_redis_connection('default') cart_key = 'cart_%d' % user.id cart_count = conn.hlen(cart_key) context.update(cart_count=cart_count) return render(request, 'index.html', context)