def task_prerun_handler(*args, **kwargs): """ Before each Task is ran, we have to instantiate Johnny's query cache monkey patch. This will make sure that any table writes invalidate table caches, and reads pull from any existing caches. """ get_backend().patch()
def cache_on_models(*models): """ Returns a decorator that allows a function to be cached on the given set of models. If any of the models is invalidated (i.e. its generation key changes), the decorated function will be re-run and re-cached. """ backend = johnny_cache.get_backend() table_names = map(lambda model: model._meta.db_table, models) def _cache_on_tables_decorator(fn): multi_generation_key = backend.keyhandler.get_generation(*table_names) fn_key = '%s.%s' % (fn.__module__, fn.__name__) def _fn_with_caching(*args, **kwargs): """ Wraps the given function, fn, with caching. """ key_parts = [multi_generation_key, fn_key, args, kwargs] key = json.dumps(key_parts) key = base64.b64encode(key) value = django_cache.get(key) if value == None: value = fn(*args, **kwargs) # pass timeout of 0 to cache forever django_cache.set(key, value, 0) return value return _fn_with_caching return _cache_on_tables_decorator
def test_savepoint_localstore_flush(self): """ This is a very simple test to see if savepoints will actually be committed, i.e. flushed out from localstore into cache. """ transaction.enter_transaction_management() managed() TABLE_NAME = 'test_table' cache_backend = cache.get_backend() cache_backend.patch() keyhandler = cache_backend.keyhandler keygen = keyhandler.keygen tm = cache_backend.cache_backend # First, we set one key-val pair generated for our non-existing table. table_key = keygen.gen_table_key(TABLE_NAME) tm.set(table_key, 'val1') # Then we create a savepoint. # The key-value pair is moved into 'trans_sids' item of localstore. tm._create_savepoint('savepoint1') # We then commit all the savepoints (i.e. only one in this case) # The items stored in 'trans_sids' should be moved back to the # top-level dictionary of our localstore tm._commit_all_savepoints() # And this checks if it actually happened. self.assertTrue(table_key in tm.local)
def newf(*args, **kwargs): backend = get_backend() was_patched = backend._patched get_backend().patch() #since this function takes all keyword arguments, #we will pass only the ones the function below accepts, just as celery does supported_keys = fun_takes_kwargs(f, kwargs) new_kwargs = dict((key, val) for key, val in kwargs.items() if key in supported_keys) try: ret = f(*args, **new_kwargs) finally: local.clear() if not was_patched: get_backend().unpatch() return ret
def handle(self, **options): print 'Registering plugins...' register_all_plugins(verbose=True) # cache invalidation print 'Invalidating plugins cache...' query_cache_backend = cache.get_backend() query_cache_backend.patch() cache.invalidate(RegisteredPlugin._meta.db_table) print 'Done.'
def search(self, term, replacement='', case_sensitive=False): if cache: backend = cache.get_backend() backend.unpatch() for model, options in self.models.items(): info = build_info(model, options, term, replacement, case_sensitive) yield info, list(options.search(term, replacement, case_sensitive)) if cache: backend.patch()
def newf(*args, **kwargs): backend = get_backend() was_patched = backend._patched get_backend().patch() # since this function takes all keyword arguments, # we will pass only the ones the function below accepts, # just as celery does supported_keys = fun_takes_kwargs(f, kwargs) new_kwargs = dict((key, val) for key, val in kwargs.items() if key in supported_keys) try: ret = f(*args, **new_kwargs) finally: local.clear() if not was_patched: get_backend().unpatch() return ret
def replace(self, queryset, term, replacement, case_sensitive): if cache: backend = cache.get_backend() backend.unpatch() self.models[queryset.model].replace(queryset, term, replacement, case_sensitive) if cache: backend.patch() cache.invalidate(queryset.model)
def __init__(self): self.__dict__ = self.__state self.disabled = settings.DISABLE_QUERYSET_CACHE self.installed = getattr(self, 'installed', False) if not self.installed and not self.disabled: # when we install, lets refresh the blacklist, just in case johnny # was loaded before the setting exists somehow... cache.blacklist = settings.BLACKLIST self.query_cache_backend = cache.get_backend() self.query_cache_backend.patch() self.installed = True
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): # Disable Johnny cache backend because it does weird things. See #852 query_cache_backend = cache.get_backend() query_cache_backend.unpatch() query_cache_backend.flush_query_cache() settings.CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', } } return django_run_tests(test_labels, verbosity, interactive, extra_tests)
def forwards(self, orm): "Write your forwards methods here." from django.conf import settings from johnny import cache from merengue.pluggable import register_plugin from merengue.pluggable.utils import install_plugin # Unpatch cache backend to disable johnny cache. Johny does weird things query_cache_backend = cache.get_backend() query_cache_backend.unpatch() from django.db import transaction for plug_dir in settings.DEMO_PLUGINS: plugin = register_plugin(plug_dir) plugin.installed = True plugin.active = True plugin.save() install_plugin(plugin) transaction.commit()
from django.conf import settings if 'johnny.middleware.QueryCacheMiddleware' in settings.MIDDLEWARE_CLASSES: from johnny.cache import get_backend from johnny.settings import MIDDLEWARE_SECONDS jc_backend = get_backend() else: jc_backend = None MIDDLEWARE_SECONDS = None
def process_exception(self, *args, **kwargs): cache.local.clear() cache.get_backend().cache_backend.rollback()
def process_response(self, req, resp): cache.local.clear() cache.get_backend().cache_backend.rollback() return resp