Coder Social home page Coder Social logo

suor / django-cacheops Goto Github PK

View Code? Open in Web Editor NEW
2.1K 40.0 223.0 1.33 MB

A slick ORM cache with automatic granular event-driven invalidation.

License: BSD 3-Clause "New" or "Revised" License

Python 96.10% Lua 3.71% Shell 0.19%
django caching python orm

django-cacheops's Introduction

Cacheops Build Status

A slick app that supports automatic or manual queryset caching and automatic granular event-driven invalidation.

It uses redis as backend for ORM cache and redis or filesystem for simple time-invalidated one.

And there is more to it:

  • decorators to cache any user function or view as a queryset or by time
  • extensions for django and jinja2 templates
  • transparent transaction support
  • dog-pile prevention mechanism
  • a couple of hacks to make django faster

Python 3.7+, Django 3.2+ and Redis 4.0+.

Using pip:

$ pip install django-cacheops

# Or from github directly
$ pip install git+https://github.com/Suor/django-cacheops.git@master

Add cacheops to your INSTALLED_APPS.

Setup redis connection and enable caching for desired models:

CACHEOPS_REDIS = {
    'host': 'localhost', # redis-server is on same machine
    'port': 6379,        # default redis port
    'db': 1,             # SELECT non-default redis database
                         # using separate redis db or redis instance
                         # is highly recommended

    'socket_timeout': 3,   # connection timeout in seconds, optional
    'password': '...',     # optional
    'unix_socket_path': '' # replaces host and port
}

# Alternatively the redis connection can be defined using a URL:
CACHEOPS_REDIS = "redis://localhost:6379/1"
# or
CACHEOPS_REDIS = "unix://path/to/socket?db=1"
# or with password (note a colon)
CACHEOPS_REDIS = "redis://:password@localhost:6379/1"

# If you want to use sentinel, specify this variable
CACHEOPS_SENTINEL = {
    'locations': [('localhost', 26379)], # sentinel locations, required
    'service_name': 'mymaster',          # sentinel service name, required
    'socket_timeout': 0.1,               # connection timeout in seconds, optional
    'db': 0                              # redis database, default: 0
    ...                                  # everything else is passed to Sentinel()
}

# Use your own redis client class, should be compatible or subclass redis.Redis
CACHEOPS_CLIENT_CLASS = 'your.redis.ClientClass'

CACHEOPS = {
    # Automatically cache any User.objects.get() calls for 15 minutes
    # This also includes .first() and .last() calls,
    # as well as request.user or post.author access,
    # where Post.author is a foreign key to auth.User
    'auth.user': {'ops': 'get', 'timeout': 60*15},

    # Automatically cache all gets and queryset fetches
    # to other django.contrib.auth models for an hour
    'auth.*': {'ops': {'fetch', 'get'}, 'timeout': 60*60},

    # Cache all queries to Permission
    # 'all' is an alias for {'get', 'fetch', 'count', 'aggregate', 'exists'}
    'auth.permission': {'ops': 'all', 'timeout': 60*60},

    # Enable manual caching on all other models with default timeout of an hour
    # Use Post.objects.cache().get(...)
    #  or Tags.objects.filter(...).order_by(...).cache()
    # to cache particular ORM request.
    # Invalidation is still automatic
    '*.*': {'ops': (), 'timeout': 60*60},

    # And since ops is empty by default you can rewrite last line as:
    '*.*': {'timeout': 60*60},

    # NOTE: binding signals has its overhead, like preventing fast mass deletes,
    #       you might want to only register whatever you cache and dependencies.

    # Finally you can explicitely forbid even manual caching with:
    'some_app.*': None,
}

You can configure default profile setting with CACHEOPS_DEFAULTS. This way you can rewrite the config above:

CACHEOPS_DEFAULTS = {
    'timeout': 60*60
}
CACHEOPS = {
    'auth.user': {'ops': 'get', 'timeout': 60*15},
    'auth.*': {'ops': ('fetch', 'get')},
    'auth.permission': {'ops': 'all'},
    '*.*': {},
}

Using '*.*' with non-empty ops is not recommended since it will easily cache something you don't intent to or even know about like migrations tables. The better approach will be restricting by app with 'app_name.*'.

Besides ops and timeout options you can also use:

local_get: True
To cache simple gets for this model in process local memory. This is very fast, but is not invalidated in any way until process is restarted. Still could be useful for extremely rarely changed things.
cache_on_save=True | 'field_name'
To write an instance to cache upon save. Cached instance will be retrieved on .get(field_name=...) request. Setting to True causes caching by primary key.

Additionally, you can tell cacheops to degrade gracefully on redis fail with:

CACHEOPS_DEGRADE_ON_FAILURE = True

There is also a possibility to make all cacheops methods and decorators no-op, e.g. for testing:

from django.test import override_settings

@override_settings(CACHEOPS_ENABLED=False)
def test_something():
    # ...
    assert cond
Automatic caching

It's automatic you just need to set it up.

Manual caching

You can force any queryset to use cache by calling its .cache() method:

Article.objects.filter(tag=2).cache()

Here you can specify which ops should be cached for the queryset, for example, this code:

qs = Article.objects.filter(tag=2).cache(ops=['count'])
paginator = Paginator(objects, ipp)
articles = list(pager.page(page_num)) # hits database

will cache count call in Paginator but not later articles fetch. There are five possible actions - get, fetch, count, aggregate and exists. You can pass any subset of this ops to .cache() method even empty - to turn off caching. There is, however, a shortcut for the latter:

qs = Article.objects.filter(visible=True).nocache()
qs1 = qs.filter(tag=2)       # hits database
qs2 = qs.filter(category=3)  # hits it once more

It is useful when you want to disable automatic caching on particular queryset.

You can also override default timeout for particular queryset with .cache(timeout=...).

Function caching

You can cache and invalidate result of a function the same way as a queryset. Cached results of the next function will be invalidated on any Article change, addition or deletion:

from cacheops import cached_as

@cached_as(Article, timeout=120)
def article_stats():
    return {
        'tags': list(Article.objects.values('tag').annotate(Count('id')))
        'categories': list(Article.objects.values('category').annotate(Count('id')))
    }

Note that we are using list on both querysets here, it's because we don't want to cache queryset objects but their results.

Also note that if you want to filter queryset based on arguments, e.g. to make invalidation more granular, you can use a local function:

def articles_block(category, count=5):
    qs = Article.objects.filter(category=category)

    @cached_as(qs, extra=count)
    def _articles_block():
        articles = list(qs.filter(photo=True)[:count])
        if len(articles) < count:
            articles += list(qs.filter(photo=False)[:count-len(articles)])
        return articles

    return _articles_block()

We added extra here to make different keys for calls with same category but different count. Cache key will also depend on function arguments, so we could just pass count as an argument to inner function. We also omitted timeout here, so a default for the model will be used.

Another possibility is to make function cache invalidate on changes to any one of several models:

@cached_as(Article.objects.filter(public=True), Tag)
def article_stats():
    return {...}

As you can see, we can mix querysets and models here.

View caching

You can also cache and invalidate a view as a queryset. This works mostly the same way as function caching, but only path of the request parameter is used to construct cache key:

from cacheops import cached_view_as

@cached_view_as(News)
def news_index(request):
    # ...
    return render(...)

You can pass timeout, extra and several samples the same way as to @cached_as(). Note that you can pass a function as extra:

@cached_view_as(News, extra=lambda req: req.user.is_staff)
def news_index(request):
    # ... add extra things for staff
    return render(...)

A function passed as extra receives the same arguments as the cached function.

Class based views can also be cached:

class NewsIndex(ListView):
    model = News

news_index = cached_view_as(News, ...)(NewsIndex.as_view())

Cacheops uses both time and event-driven invalidation. The event-driven one listens on model signals and invalidates appropriate caches on Model.save(), .delete() and m2m changes.

Invalidation tries to be granular which means it won't invalidate a queryset that cannot be influenced by added/updated/deleted object judging by query conditions. Most of the time this will do what you want, if it won't you can use one of the following:

from cacheops import invalidate_obj, invalidate_model, invalidate_all

invalidate_obj(some_article)  # invalidates queries affected by some_article
invalidate_model(Article)     # invalidates all queries for model
invalidate_all()              # flush redis cache database

And last there is invalidate command:

./manage.py invalidate articles.Article.34  # same as invalidate_obj
./manage.py invalidate articles.Article     # same as invalidate_model
./manage.py invalidate articles   # invalidate all models in articles

And the one that FLUSHES cacheops redis database:

./manage.py invalidate all

Don't use that if you share redis database for both cache and something else.

Turning off and postponing invalidation

There is also a way to turn off invalidation for a while:

from cacheops import no_invalidation

with no_invalidation:
    # ... do some changes
    obj.save()

Also works as decorator:

@no_invalidation
def some_work(...):
    # ... do some changes
    obj.save()

Combined with try ... finally it could be used to postpone invalidation:

try:
    with no_invalidation:
        # ...
finally:
    invalidate_obj(...)
    # ... or
    invalidate_model(...)

Postponing invalidation can speed up batch jobs.

Mass updates

Normally qs.update(...) doesn't emit any events and thus doesn't trigger invalidation. And there is no transparent and efficient way to do that: trying to act on conditions will invalidate too much if update conditions are orthogonal to many queries conditions, and to act on specific objects we will need to fetch all of them, which QuerySet.update() users generally try to avoid.

In the case you actually want to perform the latter cacheops provides a shortcut:

qs.invalidated_update(...)

Note that all the updated objects are fetched twice, prior and post the update.

To cache result of a function call or a view for some time use:

from cacheops import cached, cached_view

@cached(timeout=number_of_seconds)
def top_articles(category):
    return ... # Some costly queries

@cached_view(timeout=number_of_seconds)
def top_articles(request, category=None):
    # Some costly queries
    return HttpResponse(...)

@cached() will generate separate entry for each combination of decorated function and its arguments. Also you can use extra same way as in @cached_as(), most useful for nested functions:

@property
def articles_json(self):
    @cached(timeout=10*60, extra=self.category_id)
    def _articles_json():
        ...
        return json.dumps(...)

    return _articles_json()

You can manually invalidate or update a result of a cached function:

top_articles.invalidate(some_category)
top_articles.key(some_category).set(new_value)

To invalidate cached view you can pass absolute uri instead of request:

top_articles.invalidate('http://example.com/page', some_category)

Cacheops also provides get/set primitives for simple cache:

from cacheops import cache

cache.set(cache_key, data, timeout=None)
cache.get(cache_key)
cache.delete(cache_key)

cache.get will raise CacheMiss if nothing is stored for given key:

from cacheops import cache, CacheMiss

try:
    result = cache.get(key)
except CacheMiss:
    ... # deal with it

File based cache can be used the same way as simple time-invalidated one:

from cacheops import file_cache

@file_cache.cached(timeout=number_of_seconds)
def top_articles(category):
    return ... # Some costly queries

@file_cache.cached_view(timeout=number_of_seconds)
def top_articles(request, category):
    # Some costly queries
    return HttpResponse(...)

# later, on appropriate event
top_articles.invalidate(some_category)
# or
top_articles.key(some_category).set(some_value)

# primitives
file_cache.set(cache_key, data, timeout=None)
file_cache.get(cache_key)
file_cache.delete(cache_key)

It has several improvements upon django built-in file cache, both about high load. First, it's safe against concurrent writes. Second, it's invalidation is done as separate task, you'll need to call this from crontab for that to work:

/path/manage.py cleanfilecache
/path/manage.py cleanfilecache /path/to/non-default/cache/dir

Cacheops provides tags to cache template fragments. They mimic @cached_as and @cached decorators, however, they require explicit naming of each fragment:

{% load cacheops %}

{% cached_as <queryset> <timeout> <fragment_name> [<extra1> <extra2> ...] %}
    ... some template code ...
{% endcached_as %}

{% cached <timeout> <fragment_name> [<extra1> <extra2> ...] %}
    ... some template code ...
{% endcached %}

You can use None for timeout in @cached_as to use it's default value for model.

To invalidate cached fragment use:

from cacheops import invalidate_fragment

invalidate_fragment(fragment_name, extra1, ...)

If you have more complex fragment caching needs, cacheops provides a helper to make your own template tags which decorate a template fragment in a way analogous to decorating a function with @cached or @cached_as. This is experimental feature for now.

To use it create myapp/templatetags/mycachetags.py and add something like this there:

from cacheops import cached_as, CacheopsLibrary

register = CacheopsLibrary()

@register.decorator_tag(takes_context=True)
def cache_menu(context, menu_name):
    from django.utils import translation
    from myapp.models import Flag, MenuItem

    request = context.get('request')
    if request and request.user.is_staff():
        # Use noop decorator to bypass caching for staff
        return lambda func: func

    return cached_as(
        # Invalidate cache if any menu item or a flag for menu changes
        MenuItem,
        Flag.objects.filter(name='menu'),
        # Vary for menu name and language, also stamp it as "menu" to be safe
        extra=("menu", menu_name, translation.get_language()),
        timeout=24 * 60 * 60
    )

@decorator_tag here creates a template tag behaving the same as returned decorator upon wrapped template fragment. Resulting template tag could be used as follows:

{% load mycachetags %}

{% cache_menu "top" %}
    ... the top menu template code ...
{% endcache_menu %}

... some template code ..

{% cache_menu "bottom" %}
    ... the bottom menu template code ...
{% endcache_menu %}

Add cacheops.jinja2.cache to your extensions and use:

{% cached_as <queryset> [, timeout=<timeout>] [, extra=<key addition>] %}
    ... some template code ...
{% endcached_as %}

or

{% cached [timeout=<timeout>] [, extra=<key addition>] %}
    ...
{% endcached %}

Tags work the same way as corresponding decorators.

Cacheops transparently supports transactions. This is implemented by following simple rules:

  1. Once transaction is dirty (has changes) caching turns off. The reason is that the state of database at this point is only visible to current transaction and should not affect other users and vice versa.
  2. Any invalidating calls are scheduled to run on the outer commit of transaction.
  3. Savepoints and rollbacks are also handled appropriately.

Mind that simple and file cache don't turn itself off in transactions but work as usual.

There is optional locking mechanism to prevent several threads or processes simultaneously performing same heavy task. It works with @cached_as() and querysets:

@cached_as(qs, lock=True)
def heavy_func(...):
    # ...

for item in qs.cache(lock=True):
    # ...

It is also possible to specify lock: True in CACHEOPS setting but that would probably be a waste. Locking has no overhead on cache hit though.

By default cacheops considers query result is same for same query, not depending on database queried. That could be changed with db_agnostic cache profile option:

CACHEOPS = {
    'some.model': {'ops': 'get', 'db_agnostic': False, 'timeout': ...}
}

Cacheops provides a way to share a redis instance by adding prefix to cache keys:

CACHEOPS_PREFIX = lambda query: ...
# or
CACHEOPS_PREFIX = 'some.module.cacheops_prefix'

A most common usage would probably be a prefix by host name:

# get_request() returns current request saved to threadlocal by some middleware
cacheops_prefix = lambda _: get_request().get_host()

A query object passed to callback also enables reflection on used databases and tables:

def cacheops_prefix(query):
    query.dbs    # A list of databases queried
    query.tables # A list of tables query is invalidated on

    if set(query.tables) <= HELPER_TABLES:
        return 'helper:'
    if query.tables == ['blog_post']:
        return 'blog:'

Cacheops uses pickle by default, employing it's default protocol. But you can specify your own it might be any module or a class having .dumps() and .loads() functions. For example you can use dill instead, which can serialize more things like anonymous functions:

CACHEOPS_SERIALIZER = 'dill'

One less obvious use is to fix pickle protocol, to use cacheops cache across python versions:

import pickle

class CACHEOPS_SERIALIZER:
    dumps = lambda data: pickle.dumps(data, 3)
    loads = pickle.loads

Cacheops offers an "insideout" mode, which idea is instead of conj sets contatining cache keys, cache values contain a checksum of random stamps stored in conj keys, which are checked on each read to stay the same. To use that add to settings:

CACHEOPS_INSIDEOUT = True  # Might become default in future

And set up maxmemory and maxmemory-policy in redis config:

maxmemory 4gb
maxmemory-policy volatile-lru  # or other volatile-*

Note that using any of allkeys-* policies might drop important invalidation structures of cacheops and lead to stale cache.

This does not apply to "insideout" mode. This issue doesn't happen there.

In some cases, cacheops may leave some conjunction keys of expired cache keys in redis without being able to invalidate them. Those will still expire with age, but in the meantime may cause issues like slow invalidation (even "BUSY Redis ...") and extra memory usage. To prevent that it is advised to not cache complex queries, see Perfomance tips, 5.

Cacheops ships with a cacheops.reap_conjs function that can clean up these keys, ignoring conjunction sets with some reasonable size. It can be called using the reapconjs management command:

./manage.py reapconjs --chunk-size=100 --min-conj-set-size=10000  # with custom values
./manage.py reapconjs                                             # with default values (chunks=1000, min size=1000)

The command is a small wrapper that calls a function with the main logic. You can also call it from your code, for example from a Celery task:

from cacheops import reap_conjs

@app.task
def reap_conjs_task():
    reap_conjs(
        chunk_size=2000,
        min_conj_set_size=100,
    )

Cacheops provides cache_read and cache_invalidated signals for you to keep track.

Cache read signal is emitted immediately after each cache lookup. Passed arguments are: sender - model class if queryset cache is fetched, func - decorated function and hit - fetch success as boolean value.

Here is a simple stats implementation:

from cacheops.signals import cache_read
from statsd.defaults.django import statsd

def stats_collector(sender, func, hit, **kwargs):
    event = 'hit' if hit else 'miss'
    statsd.incr('cacheops.%s' % event)

cache_read.connect(stats_collector)

Cache invalidation signal is emitted after object, model or global invalidation passing sender and obj_dict args. Note that during normal operation cacheops only uses object invalidation, calling it once for each model create/delete and twice for update: passing old and new object dictionary.

  1. Conditions other than __exact, __in and __isnull=True don't make invalidation more granular.
  2. Conditions on TextFields, FileFields and BinaryFields don't make it either. One should not test on their equality anyway. See CACHEOPS_SKIP_FIELDS though.
  3. Update of "select_related" object does not invalidate cache for queryset. Use .prefetch_related() instead.
  4. Mass updates don't trigger invalidation by default. But see .invalidated_update().
  5. Sliced queries are invalidated as non-sliced ones.
  6. Doesn't work with .raw() and other sql queries.
  7. Conditions on subqueries don't affect invalidation.
  8. Doesn't work right with multi-table inheritance.

Here 1, 2, 3, 5 are part of the design compromise, trying to solve them will make things complicated and slow. 7 can be implemented if needed, but it's probably counter-productive since one can just break queries into simpler ones, which cache better. 4 is a deliberate choice, making it "right" will flush cache too much when update conditions are orthogonal to most queries conditions, see, however, .invalidated_update(). 8 is postponed until it will gain more interest or a champion willing to implement it emerges.

All unsupported things could still be used easily enough with the help of @cached_as().

Here come some performance tips to make cacheops and Django ORM faster.

  1. When you use cache you pickle and unpickle lots of django model instances, which could be slow. You can optimize django models serialization with django-pickling.

  2. Constructing querysets is rather slow in django, mainly because most of QuerySet methods clone self, then change it and return the clone. Original queryset is usually thrown away. Cacheops adds .inplace() method, which makes queryset mutating, preventing useless cloning:

    items = Item.objects.inplace().filter(category=12).order_by('-date')[:20]

    You can revert queryset to cloning state using .cloning() call. Note that this is a micro-optimization technique. Using it is only desirable in the hottest places, not everywhere.

  3. Use template fragment caching when possible, it's way more fast because you don't need to generate anything. Also pickling/unpickling a string is much faster than a list of model instances.

  4. Run separate redis instance for cache with disabled persistence. You can manually call SAVE or BGSAVE to stay hot upon server restart.

  5. If you filter queryset on many different or complex conditions cache could degrade performance (comparing to uncached db calls) in consequence of frequent cache misses. Disable cache in such cases entirely or on some heuristics which detect if this request would be probably hit. E.g. enable cache if only some primary fields are used in filter.

    Caching querysets with large amount of filters also slows down all subsequent invalidation on that model (negligable for "insideout" mode). You can disable caching if more than some amount of fields is used in filter simultaneously.

  6. Split database queries into smaller ones when you cache them. This goes against usual approach, but this allows invalidation to be more granular: smaller parts will be invalidated independently and each part will invalidate more precisely.

    Post.objects.filter(category__slug="foo")
    # A single database query, but will be invalidated not only on
    # any Category with .slug == "foo" change, but also for any Post change
    
    Post.objects.filter(category=Category.objects.get(slug="foo"))
    # Two queries, each invalidates only on a granular event:
    # either category.slug == "foo" or Post with .category_id == <whatever is there>

Writing a test for an issue you are experiencing can speed up its resolution a lot. Here is how you do that. I suppose you have some application code causing it.

  1. Make a fork.
  2. Install all from requirements-test.txt.
  3. Ensure you can run tests with pytest.
  4. Copy relevant models code to tests/models.py.
  5. Go to tests/tests.py and paste code causing exception to IssueTests.test_{issue_number}.
  6. Execute pytest -k {issue_number} and see it failing.
  7. Cut down model and test code until error disappears and make a step back.
  8. Commit changes and make a pull request.
  • faster .get() handling for simple cases such as get by pk/id, with simple key calculation
  • integrate previous one with prefetch_related()
  • shard cache between multiple redises
  • respect subqueries?
  • respect headers in @cached_view*?
  • group invalidate_obj() calls?
  • a postpone invalidation context manager/decorator?
  • fast mode: store cache in local memory, but check in with redis if it's valid
  • an interface for complex fields to extract exact on parts or transforms: ArrayField.len => field__len=?, ArrayField[0] => field__0=?, JSONField['some_key'] => field__some_key=?
  • custom cache eviction strategy in lua
  • cache a string directly (no pickle) for direct serving (custom key function?)

django-cacheops's People

Contributors

aykut avatar bourivouh avatar crazyzubr avatar davidfb avatar eli-schwartz avatar elmit avatar emilstenstrom avatar erthalion avatar georgepasturemap avatar ihucos avatar ir4y avatar itcrab avatar jeremystretch avatar jhillacre avatar libram avatar lokhman avatar m1ha-shvn avatar michalochman avatar mikevl avatar mjnaderi avatar mvbrn avatar nicwolff avatar prokaktus avatar ron8mcr avatar suor avatar timgates42 avatar timsavage avatar ttys15 avatar tumb1er avatar yuego avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-cacheops's Issues

Проблемы валидности кеша в консольных скриптах

Кеш по каким-то причинам не инвалидируется, если запускать консольные скрипты.
Обновляю или добавляю записи, но на веб морде изменений нет. Такое впечатление, что это два разных кеша. При чем в консоли кеш даже после манипуляций не инвалидируется. И при повторном запуске скрипт фейлиться с эксепшенами. Так как пытается манипулировать с уже не существующими обьектами.
Приходится при старте скрипта инвалидировать кеш , через dbflush . Соотвественно и при его завершении. Думаю стоит протестировать, и поискать причины. Так же не очень приятно писать импорт библиотеки, что бы запустить скрипт. Все это как-то не трушно. А в целом утилита добрая))

Watched variable changed

We're encountering the following problem with cacheops on various places in our site. It all started after we changed wsgi settings (see below) to fight memory leaks. In particular, we blame added multithreading.

Traceback (most recent call last):

  File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  ...

  File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 82, in __len__
    self._result_cache = list(self.iterator())

  File "/usr/lib/python2.6/site-packages/cacheops/query.py", line 327, in iterator
    self._cache_results(cache_key, results)

  File "/usr/lib/python2.6/site-packages/cacheops/query.py", line 231, in _cache_results
    cache_thing(self.model, cache_key, results, cond_dnf, timeout=self._cachetimeout)

  File "/usr/lib/python2.6/site-packages/cacheops/query.py", line 57, in cache_thing
    txn.execute()

  File "/usr/lib/python2.6/site-packages/redis/client.py", line 1515, in execute
    return execute(conn, stack)

  File "/usr/lib/python2.6/site-packages/redis/client.py", line 1465, in _execute_transaction
    raise WatchError("Watched variable changed.")

WatchError: Watched variable changed.

Here's out set up:

Apache/2.2.16
mod_wsgi/3.3
Python/2.6.7
cacheops/0.9.1

httpd.conf

WSGIDaemonProcess test processes=4 threads=15 maximum-requests=200 stack-size=1048576 display-name=%{GROUP}
WSGIProcessGroup test
WSGISocketPrefix /var/run/wsgi
WSGIRestrictEmbedded On

settings.py

CACHEOPS_REDIS = {
    'host': '127.0.0.1',
    'port': 6379,
    'db': 1,
    'socket_timeout': 3,
}

CACHEOPS = {
        '*.*': ('all', 60*15),
}

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': '127.0.0.1:6379',
        'OPTIONS': {
            'DB': 3,
            'PASSWORD': '',
            'PARSER_CLASS': DefaultParser
        },
    },
}

wrong conj_keys for query in django.contrib.auth

In django.contrib.auth in model User in method get_profile()
formed query for retrieve Profile. like:

Profile.objects.get(user__id__exact=123)

its equal for query:

Profile.objects.get(user=123)

but their conj_keys is not equal:
for first query - key is "conj:profile.profile:id=123"
for second query - key is "conj:profile.profile:user_id=123"

and first key is wrong (must be "user_id=123" or something like this, but not "id=123")

used django 1.3.1 and last version of cacheops from 16.04

Cacheops don't invalidate on old state when force_update=True is used

Currently fetching old object relies on patching QuerySet.exists() to perform full get and stash a result. Normally .exists() is called in Model.pre_save() which makes things work. The thing is .exists() is not called when force_update=True is used.

Additional misfeature of current hacky approach is slowering all .exists() calls.

can't cache model methods

I'm not sure if caching model methods is an existing capability, or whether it should be. When I decorate a method of a model class with @cached(timeout=60), and then I call the instance method, it seems to try and cache the model instance rather than the method. I get this error:

'MyModel' object has no attribute 'name'

Exception Location: /usr/lib/python2.7/functools.py in update_wrapper, line 33

3rd party apps breaking

Been using this app called feedjack (http://www.feedjack.org/) to grab feeds

Traceback (most recent call last):
  File "feedjack_update.py", line 505, in <module>
    main()
  File "feedjack_update.py", line 488, in main
    for feed in models.Feed.objects.filter(is_active=True):
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 107, in _result_iter
    self._fill_cache()
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 772, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/usr/local/lib/python2.6/dist-packages/cacheops/query.py", line 301, in iterator
    cache_this = self._cacheprofile is not None and 'fetch' in self._cacheops
AttributeError: 'QuerySet' object has no attribute '_cacheprofile'

I'm not caching any of the feed jack models

CACHEOPS = {
    'pad.padportfoliothumb': ('all', 60*60),
    'pad.*':  ('just_enable', 60*60),
}

Invalidate cached function

Hi, if I cache a function with

@cached()
def start_time(flight):
    outp = complicated_stuff(flight.x)
    return outp

Is there an easy way I can invalidate it?

Are Django's cache framework and django-cacheops mutually exclusive?

Can I use Django's cache framework at the same time as django-cacheops?

I would like to still use the Django's cache framework, with memcached backend, to do some page's caching, while using django-cacheops as ORM cache.

Is there any problem? Should I do otherwise? Is it counterproductive?

TypeError: __new__() takes exactly 3 arguments (2 given)

Hello!

I use django-cacheops method .cache() in my project

One of the model always raise the error: TypeError: new() takes exactly 3 arguments (2 given)

Traceback (most recent call last):
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 107, in get_response
    response = middleware_method(request, callback, callback_args, callback_kwargs)
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/debug_toolbar/middleware.py", line 73, in process_view
    response = panel.process_view(request, view_func, view_args, view_kwargs)
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/debug_toolbar/panels/profiling.py", line 133, in process_view
    return self.profiler.runcall(view_func, *args, **view_kwargs)
  File "/usr/lib/python2.7/cProfile.py", line 149, in runcall
    return func(*args, **kw)
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "/vagrant/source/ilovei/articles/views.py", line 106, in get
    arts = list(context["object_list"].cache()) #hit
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/vagrant/my-venv/local/lib/python2.7/site-packages/cacheops/query.py", line 367, in iterator
    results = pickle.loads(cache_data)
TypeError: __new__() takes exactly 3 arguments (2 given)

The same if i cache this model by simple cache:

from cacheops import cache
        tmp = None
        try:
            tmp = cache.get("cache_key")
        except CacheMiss:
            print "=====+CacheMiss+====="
        if tmp is None:
            tmp = list(Article.objects.all())
        cache.set("cache_key", tmp, timeout=60)

I use Django 1.6.1 and default settings cacheops from reedme.rst. And i don’t have any problems with caching other models in this project

Could you help me to resolve it?

Thank you.

Multiple monkey mix not supported

Hello!

First of all, thank you for this great app!

But we have some problems with using it. This issue catched only on staging server, not on local machines.

When we add cacheops to INSTALLED_APPS, your app raise error:

AssertionError at /
Multiple monkey mix not supported

http://dpaste.org/QfoUZ/

I added some log:

logger.debug(cls)
if '_no_monkey' in cls.dict:
--logger.debug('catch')
assert '_no_monkey' not in cls.dict, 'Multiple monkey mix not supported'

and this is output:

DEBUG utils class 'django.db.models.manager.Manager'
DEBUG utils 139989483362048 catch

For now I don't understand this. Could you help me?

RuntimeError: dictionary changed size during iteration

Hello,

I have a project which extends django-oscar and I use django-cacheops for caching.

While I'm trying to cache my product objects, I'm having the following exception.

RuntimeError                              Traceback (most recent call last)
<ipython-input-1-44863237a714> in <module>()
----> 1 p=Product.objects.get(id=53030)

/Users/aykutozat/dhr/lib/python2.7/site-packages/cacheops/query.pyc in get(self, *args, **kwargs)
    522
    523     def get(self, *args, **kwargs):
--> 524         return self.get_queryset().inplace().get(*args, **kwargs)
    525
    526     def cache(self, *args, **kwargs):

/Users/aykutozat/dhr/lib/python2.7/site-packages/cacheops/query.pyc in get(self, *args, **kwargs)
    414             qs = self
    415
--> 416         return qs._no_monkey.get(qs, *args, **kwargs)
    417
    418     def exists(self):

/Users/aykutozat/dhr/lib/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    359         if self.query.can_filter():
    360             clone = clone.order_by()
--> 361         num = len(clone)
    362         if num == 1:
    363             return clone._result_cache[0]

/Users/aykutozat/dhr/lib/python2.7/site-packages/django/db/models/query.pyc in __len__(self)
     83                 self._result_cache = list(self._iter)
     84             else:
---> 85                 self._result_cache = list(self.iterator())
     86         elif self._iter:
     87             self._result_cache.extend(self._iter)

/Users/aykutozat/dhr/lib/python2.7/site-packages/cacheops/query.pyc in iterator(self)
    372
    373         if cache_this:
--> 374             self._cache_results(cache_key, results)
    375         raise StopIteration
    376

/Users/aykutozat/dhr/lib/python2.7/site-packages/cacheops/query.pyc in _cache_results(self, cache_key, results, timeout)
    275     def _cache_results(self, cache_key, results, timeout=None):
    276         cond_dnf = dnf(self)
--> 277         cache_thing(self.model, cache_key, results, cond_dnf, timeout or self._cachetimeout)
    278
    279     def cache(self, ops=None, timeout=None, write_only=None):

/Users/aykutozat/dhr/lib/python2.7/site-packages/cacheops/conf.pyc in _inner(*args, **kwargs)
     34     def _inner(*args, **kwargs):
     35         try:
---> 36             return func(*args, **kwargs)
     37         except redis.ConnectionError, e:
     38             warnings.warn("The cacheops cache is unreachable! Error: %s" % e, RuntimeWarning)

/Users/aykutozat/dhr/lib/python2.7/site-packages/cacheops/query.pyc in cache_thing(model, cache_key, data, cond_dnf, timeout)
     48
     49     # Write data to cache
---> 50     pickled_data = pickle.dumps(data, -1)
     51     if timeout is not None:
     52         txn.setex(cache_key, timeout, pickled_data)

RuntimeError: dictionary changed size during iteration

I only get this error for this particular model(Product). When I try to pickle my product object with pickle rather than cPickle, I don't get this error. I'm not sure the problem is in cPickle or something else, so I need your help.

My Django version is 1.4.10, django-cacheops verson is 1.1 and here is my settings for cacheops.

CACHEOPS_REDIS = {
    'host': 'localhost', # redis-server is on same machine
    'port': 6379,        # default redis port
    'db': 1,             # SELECT non-default redis database
                         # using separate redis db or redis instance
                         # is highly recommended
    'socket_timeout': 3,
}

CACHEOPS = {
    'catalogue.product': ('get', 60*15),
}
CACHEOPS_DEGRADE_ON_FAILURE=True

Thanks.

Error on production

Hello Suor,

I'm getting the following error on production only (sending an email works fine locally):

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/deploy/corgi/corgiapp/decorators.py", line 70, in validate_logged_in
    return fn(request, *args, **kwargs)

  File "/home/deploy/corgi/corgiapp/decorators.py", line 14, in validate_corgi
    return fn(request, corgi_user, *args, **kwargs)

  File "/home/deploy/corgi/corgiapp/decorators.py", line 56, in validate_instructor_or_ta
    return fn(request, corgi_user, *args, **kwargs)

  File "/home/deploy/corgi/corgiapp/views/roster.py", line 48, in roster
    send_email.send_added_to_corgi_email(request, corgi_users)

  File "/home/deploy/corgi/corgiapp/views/send_email.py", line 122, in send_added_to_corgi_email
    _send_added_to_corgi_email(request, send_to)

  File "/home/deploy/corgi/corgiapp/views/send_email.py", line 61, in _send_added_to_corgi_email
    current_site = get_current_site(request)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/django/contrib/sites/models.py", line 95, in get_current_site
    current_site = Site.objects.get_current()

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/django/contrib/sites/models.py", line 26, in get_current
    current_site = self.get(pk=sid)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/cacheops/query.py", line 532, in get
    return self.get_queryset().inplace().get(*args, **kwargs)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/cacheops/query.py", line 423, in get
    return qs._no_monkey.get(qs, *args, **kwargs)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 382, in get
    num = len(clone)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 90, in __len__
    self._result_cache = list(self.iterator())

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/cacheops/query.py", line 381, in iterator
    self._cache_results(cache_key, results)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/cacheops/query.py", line 284, in _cache_results
    cache_thing(self.model, cache_key, results, cond_dnf, timeout or self._cachetimeout)

  File "/home/deploy/corgi/venv/local/lib/python2.7/site-packages/cacheops/query.py", line 67, in cache_thing
    txn.expire(conj_key, model._cacheprofile['timeout'] + 10)

AttributeError: type object 'Site' has no attribute '_cacheprofile'

In case it's useful, my settings are:

CACHEOPS_REDIS = {
   'host': 'localhost',
   'port': 6379,
   'db': 1,
   'socket_timeout': 3,
}

CACHEOPS = {
   'auth.user': ('get', 60*15),
   'auth.*': ('all', 60*60),
   '*.*': ('all', 60*120),
}

Any ideas what the issue may be? Cacheops is working well in other parts of the code. Thanks in advance.

ResponseError: unknown command 'MULTI'

Hi!

After install cacheopts I have this issue:
unknown command 'MULTI'

Installation process:
sudo apt-get install redis-server
pip install redis
pip install django-cacheops
(later from git: pip install -e git+git://github.com/Suor/django-cacheops.git#egg=django-cacheops)

And my config is default:
CACHEOPS_REDIS = {
'host': 'localhost', # redis-server is on same machine
'port': 6379, # default redis port
'db': 1, # SELECT non-default redis database
# using separate redis db or redis instance
# is highly recommended
'socket_timeout': 3,
}

CACHEOPS = {
# Automatically cache any User.objects.get() calls for 15 minutes
# This includes request.user or post.author access,
# where Post.author is a foreign key to auth.User
'auth.user': ('get', 60*15),

# Automatically cache all gets, queryset fetches and counts
# to other django.contrib.auth models for an hour
'auth.*': ('all', 60*60),

# Enable manual caching on all news models with default timeout of an hour
# Use News.objects.cache().get(...)
#  or Tags.objects.filter(...).order_by(...).cache()
# to cache particular ORM request.
# Invalidation is still automatic
'news.*': ('just_enable', 60*60),

# Automatically cache count requests for all other models for 15 min
'*.*': ('count', 60*15),

}

Here is my traceback: http://dpaste.org/4zTLA/

I'm new to redis, sorry for probably newbie question.
Thanx.

Auth.users cannot be cached automatically

_cacheprofile is not attatched to auth.users in Django 1.3

I'm unsure why this is true. My work around is to manually instantiate _cacheprofile in my apps models.py

from django.contrib.auth.models import User
User._cacheprofile = {'local_get': False,
                      'timeout': 3600, # 1 Hour
                      'ops': set(['count', 'fetch', 'get'])}

Strange ORM issue

Hello Suor,

I'm having a problem about cacheops.

In [2]: User.objects.get?
Type:       instancemethod
String Form:<bound method UserManager.get of <django.contrib.auth.models.UserManager object at 0x10b1dd590>>
File:       /Users/aykutozat/easy_deneme/lib/python2.7/site-packages/cacheops/query.py
Definition: User.objects.get(self, *args, **kwargs)
Docstring:  <no docstring>

In [3]: User.objects.filter?
Type:       instancemethod
String Form:<bound method UserManager.filter of <django.contrib.auth.models.UserManager object at 0x10b1dd590>>
File:       /Users/aykutozat/easy_deneme/lib/python2.7/site-packages/django/db/models/manager.py
Definition: User.objects.filter(self, *args, **kwargs)
Docstring:  <no docstring>

You can see that get method of User manager points to cacheops. However I didn't set any cache behavior for User and even for no other models. Here is my settings:

CACHEOPS_REDIS = {
    'host': 'localhost',  # redis-server is on same machine
    'port': 6379,         # default redis port
    'db': 1,              # SELECT non-default redis database
                          # using separate redis db or redis instance
                          # is highly recommended
    'socket_timeout': 3,
}

CACHEOPS = {
}

CACHEOPS_DEGRADE_ON_FAILURE = True

The biggest problem I have about this issue is below.

class ProductReview(models.Model):
    product = models.ForeignKey(
        'catalogue.Product', related_name='reviews', null=True,
        on_delete=models.SET_NULL)

class Product(models.Model):
    ....

When I try to query reviewsover any product instance, .getqueries to all table. Even I have no cacheops settings for productreview, its .get comes from under cacheops/query.py just like User above.

Here is an example to make my point more clear.

In [1]: p=Product.objects.get(id=129037)

In [2]: p.reviews.get(status=0)
Out[2]: <ProductReview: Deneme>

SELECT "reviews_productreview"."id", .... 
FROM "reviews_productreview" WHERE "reviews_productreview"."status" = 0

You see that there is no condition for product_id at where clause. When I remove cacheops from my installed-apps here is the result.

In [1]: p=Product.objects.get(id=129037)

In [2]: p.reviews.get(status=0)

SELECT "reviews_productreview"."id", ... 
FROM "reviews_productreview" WHERE ("reviews_productreview"."product_id" = 129037  AND "reviews_productreview"."status" = 0 )

When I debug the problem, I've headed to get method under ManagerMixin which queries the only given kwargs. However, even I didn't want cacheops to cache the ProductReview or User, why it still overlaps their .get?

Thanks.

Error with Django 1.6

Hi, I imagine cacheops is not supposed to work with the as-yet-unreleased Django 1.6, but I thought I'd let you know what happens when I try. I get:

AttributeError at /flight/2013-07-15-05-25-1log_2/

'WhereNode' object has no attribute 'subtree_parents'

Request Method: GET
Request URL: http://afterflight.foobarbecue.com:8002/flight/2013-07-15-05-25-1log_2/
Django Version: 1.6b4
Exception Type: AttributeError
Exception Value:

'WhereNode' object has no attribute 'subtree_parents'

Multi-table inheritance not supported

More precisely, automatic event-based doesn't work properly for it. There are actually two different cases:

  1. When SubModel instance is added, changed or deleted none of BaseModel queries invalidated.
  2. When BaseModel instance changed or deleted none of queries for any submodels are invalidated.

Doesn't invalidate for ManyToManyField with through table

class Photo(models.Model):
liked_user = ManyToManyField(User, through="PhotoLike")

class PhotoLike(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
photo = models.ForeignKey(Photo)
timestamp = models.DateTimeField(auto_now_add=True)

user list who likes photo:

Photo.objects.get(pk=1).liked_user.all()
user1, user2

after a new user likes the photo:

PhotoLike.objects.create(user=user3, photo=photo)
Photo.objects.get(pk=1).liked_user.all()
user1, user2
the result is wrong? user3 should be in the result. I see the cache isn't invalidated.
Could you help me?

"wrong number of arguments for 'watch' command" error

Wanted to try the library but no luck, got this exception:

Django Version: 1.4
Exception Type: ResponseError
Exception Value: ERR wrong number of arguments for 'watch' command
Redis Version: 2.4.4
redis==2.4.9 in requirements.txt

When doing object.save() for "get" cached model.

Can put zipped error page to some file hosting for investigation.

Result of generation conj_cache_key may be different for boolean fields

> users = User.objects.filter(is_active=False)
> len(users) 
#0 

> user = User.objects.get(pk=1)
> user.is_active 
# True

> user.is_active = False
> user.save()
> user.is_active 
# False

> users = User.objects.filter(is_active=False)
> len(users) 
#0

User.objects.filter(is_active=False) generate conj_cache_key = "conj:auth.user:is_active=False"

user.save(), while invalidation, generate conj_cache_keys for old and new version of user = "conj:auth.user:is_active=1" and "conj:auth.user:is_active=0"

"conj:auth.user:is_active=False" is not equal "conj:auth.user:is_active=1" and is not equal "conj:auth.user:is_active=0"
so len(users) returns cached result = 0 but must be 1

I think bool values must be converted to int values
in
conj_cache_key_from_scheme() (invalidation.py)
and in _dnf(where) (utils.py)

Does many to many work

I am using InheritanceManager [django-model-utils] for a few of my models.

I have a 'Folder' model and then a base 'Media' model.
The folder has a many to many to the Media

Then I have a few implementations of Media i.e Image, Document etc. Not sure if this is really an issue but when I create a new instance of Document lets say and add it to the folders many to many relationship, it doesn't seem to invalidate or im missing something.

Here is the result in shell. This might help.

folder.media.all()
Media: Media object, Media: Media object, Media: Media object, Media: Media object

list(folder.media.all())
Media: Media object, Media: Media object

The django rendering seems to do the latter looks like cause I only see two in the objects in template.

Any ideas? Thanks.

install_cacheops() fails with Django 1.4

Setup is exactly like in the installation instructions from README.

After adding cacheops cannot even runserver or any manage.py command:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/core/management/base.py", line 209, in execute
    translation.activate('en-us')
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 100, in activate
    return _trans.activate(language)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 202, in activate
    _active.value = translation(language)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 185, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
    app = import_module(appname)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/slam/workspace/taobao_shop/satchless/contrib/payment/django_payments_provider/__init__.py", line 5, in <module>
    from . import listeners
  File "/home/slam/workspace/taobao_shop/satchless/contrib/payment/django_payments_provider/listeners.py", line 2, in <module>
    from . import models
  File "/home/slam/workspace/taobao_shop/satchless/contrib/payment/django_payments_provider/models.py", line 3, in <module>
    from payments.models import Payment
  File "/home/slam/workspace/taobao_shop/payments/models.py", line 9, in <module>
    from .robox.handler import payment_failed, payment_received
  File "/home/slam/workspace/taobao_shop/payments/robox/handler.py", line 1, in <module>
    from satchless.order.models import Order
  File "/home/slam/workspace/taobao_shop/satchless/order/__init__.py", line 4, in <module>
    from . import listeners
  File "/home/slam/workspace/taobao_shop/satchless/order/listeners.py", line 1, in <module>
    from satchless.cart.signals import cart_content_changed
  File "/home/slam/workspace/taobao_shop/satchless/cart/__init__.py", line 4, in <module>
    from .handler import AddToCartHandler
  File "/home/slam/workspace/taobao_shop/satchless/cart/handler.py", line 5, in <module>
    from . import forms
  File "/home/slam/workspace/taobao_shop/satchless/cart/forms.py", line 15, in <module>
    class AddToCartForm(forms.Form, QuantityForm):
  File "/home/slam/workspace/taobao_shop/satchless/cart/forms.py", line 19, in AddToCartForm
    quantity = forms.DecimalField(_('Quantity'), initial=1)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 81, in ugettext
    return _trans.ugettext(message)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 286, in ugettext
    return do_translate(message, 'ugettext')
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 276, in do_translate
    _default = translation(settings.LANGUAGE_CODE)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 185, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 162, in _fetch
    app = import_module(appname)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/cacheops/__init__.py", line 8, in <module>
    install_cacheops()
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/cacheops/query.py", line 495, in install_cacheops
    for model in get_models(include_auto_created=True):
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/db/models/loading.py", line 167, in get_models
    self._populate()
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/db/models/loading.py", line 83, in load_app
    if not module_has_submodule(app_module, 'models'):
  File "/home/slam/.virtualenvs/taobao/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 17, in module_has_submodule
    for entry in package.__path__:  # No __path__, then not a package.
AttributeError: 'module' object has no attribute '__path__'

ManyToMany models problem in Django Admin users management

If I’ve changed something about Django user in Admin, it’ll raise exception about m2m instance have no “_cacheprofile” attribute.
CacheOps config contain 3 rows from documentation expamles that related to user model:

CACHEOPS = {
    'auth.user': ('get', 60*15),
    'auth.*': ('all', 60*60),
    # 'my_apps.*': ('all', 60*30),   
    '*.*': ('count', 60*15),
}
Traceback (most recent call last):

 File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 111, in get_response
   response = callback(request, *callback_args, **callback_kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py", line 307, in wrapper
   return self.admin_site.admin_view(view)(*args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py", line 93, in _wrapped_view
   response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
   response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py", line 197, in inner
   return view(request, *args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py", line 28, in _wrapper
   return bound_func(*args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py", line 93, in _wrapped_view
   response = view_func(request, *args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py", line 24, in bound_func
   return func(self, *args2, **kwargs2)

 File "/usr/local/lib/python2.6/dist-packages/django/db/transaction.py", line 217, in inner
   res = func(*args, **kwargs)

 File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/options.py", line 983, in change_view
   form.save_m2m()

 File "/usr/local/lib/python2.6/dist-packages/django/forms/models.py", line 82, in save_m2m
   f.save_form_data(instance, cleaned_data[f.name])

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 1149, in save_form_data
   setattr(instance, self.attname, data)

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 745, in __set__
   manager.clear()

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 519, in clear
   self._clear_items(self.source_field_name)

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py", line 641, in _clear_items
   source_field_name: self._pk_val

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 444, in delete
   collector.collect(del_query)

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/deletion.py", line 146, in collect
   reverse_dependency=reverse_dependency)

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/deletion.py", line 91, in add
   if not objs:

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 113, in __nonzero__
   iter(self).next()

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 107, in _result_iter
   self._fill_cache()

 File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 772, in _fill_cache
   self._result_cache.append(self._iter.next())

 File "/usr/local/lib/python2.6/dist-packages/cacheops/query.py", line 341, in iterator
   self._cache_results(cache_key, results)

 File "/usr/local/lib/python2.6/dist-packages/cacheops/query.py", line 241, in _cache_results
   cache_thing(self.model, cache_key, results, cond_dnf, timeout=self._cachetimeout)

 File "/usr/local/lib/python2.6/dist-packages/cacheops/query.py", line 54, in cache_thing
   txn.expire(conj_key, model._cacheprofile['timeout'] + 10)

AttributeError: type object 'User_groups' has no attribute '_cacheprofile'

Invalidating proxy models

Hi, is there correct way to invalidate proxy model objects when changed "parent" model object ?

I have:

# tihs is parent
class Video(models.Model):
    objects = VideoManager()


# this is proxy
class OembedVideo(Video, OembedVideoMixin):
    class Meta:
        proxy = True

    objects = DefaultManager()

# fix invalidation here:
class DefaultManager(VideoManager):
    def get(self, *args, **kwargs):
        """ добиваемся того, чтобы объекты OembedVideo
        инвалидировались при изменении модели Video
        По факту это одна и также модель, 
но cacheops автоматически не инвалидирует
        объекты проксируемых моделей при изменении 
объекта родительской модели """
        from cacheops import cached_as
        @cached_as(Video.objects.get(**kwargs))
        def _get():
            qs = self.get_query_set()
            return qs.nocache().get(*args, **kwargs)

        return _get()

without manager override, when i change Video model object, OembedVideo is still returns old cache.

cant use ./manage.py invalidate command by default after pip install

After installing the library through pip, in the application directory (cacheops) there is no directory "managment/commands" which must contain a file invalidate.py with the command invalidate for use in "./manage.py invalidate <all | obj | ...>" This is not a mistake?

cd .virtualenv/lib/python2.7/site-packages/cacheops/
ls -l
-rw-r--r-- 1 sq wheel 154 Dec 24 13:26 init.py
-rw-r--r-- 1 sq wheel 1939 Dec 24 13:26 conf.py
-rw-r--r-- 1 sq wheel 6389 Dec 24 13:26 invalidation.py
-rw-r--r-- 1 sq wheel 2408 Dec 24 13:26 jinja2.py
-rw-r--r-- 1 sq wheel 0 Dec 24 13:26 models.py
-rw-r--r-- 1 sq wheel 20079 Dec 24 13:26 query.py
-rw-r--r-- 1 sq wheel 4199 Dec 24 13:26 simple.py
-rw-r--r-- 1 sq wheel 4172 Dec 24 13:26 utils.py

no managment/commands here

Exception on trying to cache models with OneToOneRel

When i'm trying to cache my models which contains OneToOneField i`m getting this exception:

for obj in iter:\n\n File "C:\Users\ShapeR\webadmenv\lib\site-packages\cacheops\query.py", line 347, in iterator\n cache_key = self._cache_key()\n\n File "C:\Users\ShapeR\webadmenv\lib\site-packages\cacheops\query.py", line 255, in _cache_key\n md5.update(stringify_query(self.query))\n\n File "C:\Users\ShapeR\webadmenv\lib\site-packages\cacheops\query.py", line 213, in stringify_query\n raise ValueError(*e.args)\n\nValueError: Can't stringify django.db.models.fields.related.OneToOneRel object at 0x047918B0}

Can't encode Decimal

Hi, I have a problems, when I try to filter results by Decimal filed.
I'm getting following error:
TypeError: Can't encode Decimal('7')
Stack:
cacheops/query.py in stringify_query

            raise TypeError("Can't encode %s" % repr(obj))
    def stringify_query(query):
        return json.dumps(query, default=encode_object, skipkeys=True, sort_keys=True, separators=(',',':'))
    return stringify_query

models.py:

    class ModelName(...):
        field = models.DecimalField(blank=True, default=0.0)

views.py:

    def view(...):
        ModelName.objects.distinct().filter(field__range=[1.0, 10.0])

Can u fix it?

cacheops is not thread safe

It was never designed to be, but now that some folks are trying to use it with threads, I create this issue.

There are two things that can go wrong:

  • accidental WatchErrors described in #9
  • corruption of cache_schemes since schemes and their version update is not atomic
    (there are more shared objects, but they should not cause any issues)

The first problem should be addressed in pooling_refactor branch, second is highly unlikely to appear. Can anyone test it some real application using threads?

using models in middleware at apache mod_wsgi breaks all

settings.py

CACHEOPS_REDIS = {
    'host': 'localhost',
    'port': 6379,
    'db': 1,
}

CACHEOPS = {
    '*.*': ('all', 60*60*24),
}

traceback

django/core/handlers/base.py", line 47, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware xxx.middleware: "cannot import name Xxx"

in middleware just

from xxx.models.imort *

at development server everything is ok

PicklingError: Can't pickle User Groups

I have a signal removing and adding user groups.

Traceback:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/code/filterfoundry/ffprofile/views.py" in register_with_subscription_plan
  362.     return register(request, *args, **kwargs)
File "/home/code/filterfoundry/registration/views.py" in register
  187.             new_user = backend.register(request, **form.cleaned_data)
File "/home/code/filterfoundry/registration/backends/simple/__init__.py" in register
  33.                                      request=request)
File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py" in send
  172.             response = receiver(signal=self, sender=sender, **named)
File "/home/code/filterfoundry/ffprofile/signals.py" in new_user
  71.         object.save()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py" in save_base
  570.                 created=(not record_exists), raw=raw, using=using)
File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py" in send
  172.             response = receiver(signal=self, sender=sender, **named)
File "/home/code/filterfoundry/promocodes/signals.py" in new_promocode
  9.         subscription_change(instance.user,instance.code.subscription)
File "/home/code/filterfoundry/subscription/views.py" in subscription_change
  64.         user.groups.remove(old_group)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py" in remove
  511.                 self._remove_items(self.source_field_name, self.target_field_name, *objs)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/fields/related.py" in _remove_items
  622.                     '%s__in' % target_field_name: old_ids
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in delete
  444.         collector.collect(del_query)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/deletion.py" in collect
  146.                             reverse_dependency=reverse_dependency)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/deletion.py" in add
  91.         if not objs:
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in __nonzero__
  113.             iter(self).next()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in _result_iter
  107.                 self._fill_cache()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py" in _fill_cache
  772.                     self._result_cache.append(self._iter.next())
File "/home/code/filterfoundry/cacheops/query.py" in iterator
  328.             self._cache_results(cache_key, results)
File "/home/code/filterfoundry/cacheops/query.py" in _cache_results
  230.         cache_thing(self.model, cache_key, results, cond_dnf, timeout=self._cachetimeout)
File "/home/code/filterfoundry/cacheops/query.py" in cache_thing
  40.     pickled_data = pickle.dumps(data, -1)

Exception Type: PicklingError at /users/join/creative/
Exception Value: Can't pickle <class 'django.contrib.auth.models.User_groups'>: attribute lookup django.contrib.auth.models.User_groups failed

Configuring cacheops

Hi--

I'm using cacheops and it is awesome. I'm still relatively new to it, so am trying to figure out the best configuration to keep as many things as possible in the cache since we are having a massive uptick in traffic and the site is still quite slow. I had an issue before where I had MAX_ENTRIES set to 1000 so it did not keep entries in the cache for very long. Now, I'm thinking it may just be better to keep items in the cache for longer? I've also tried looking at the maxmemory settings in the redis config --- there are still < 100k items in the cache but things still seem to get evicted. Any tips on the best way to configure it if I just want to cache as much as possible?

CACHEOPS = {
    '*.*': ('all', 60 * 60),
 }

CACHEOPS_DEGRADE_ON_FAILURE = True

CACHES = {
    "default": {
        "BACKEND": "redis_cache.cache.RedisCache",
        "LOCATION": "127.0.0.1:6379:1",
        "OPTIONS": {
        "CLIENT_CLASS": "redis_cache.client.DefaultClient",
        "MAX_ENTRIES": 1000000,
       }
   }

}

cache_on_save is not working as expected

Hello!
I try to use cache_on_save feature.
In settings.py:

CACHEOPS = {
    'app.model': ('all', DEFAULT_CACHE_TIME, {"cache_on_save": "video"})
}

Model object cached after saving by key conj:app.model:video_id=1 - all right.
But in the next calling of get function (Model.objects.get(video=1)), Model is retrieved from database, not from cache.

It happens because _cache_key return diffrent keys.
That's because when Model is cached after saving we have in _cache_key function:

extra = "cacheops.query.<lambda>"
self.query.default_ordering == True
self.query.used_aliases == set(["app_model"])

But when Model retrieve:

extra = ""
self.query.default_ordering == False
self.query.used_aliases == set([])

TypeError: object of type 'Query' has no len()

Get the following error on a offset/limit query

File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/django/db/models/query.py", line 118, in _result_iter
self._fill_cache()
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/django/db/models/query.py", line 892, in _fill_cache
self._result_cache.append(self._iter.next())
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/query.py", line 327, in iterator
self._cache_results(cache_key, results)
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/query.py", line 230, in _cache_results
cond_dnf = dnf(self)
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/utils.py", line 107, in dnf
result = _dnf(where)
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/utils.py", line 83, in _dnf
chilren_dnfs = filter(None, imap(_dnf, where.children))
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/utils.py", line 83, in _dnf
chilren_dnfs = filter(None, imap(_dnf, where.children))
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/utils.py", line 83, in _dnf
chilren_dnfs = filter(None, imap(_dnf, where.children))
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/utils.py", line 83, in _dnf
chilren_dnfs = filter(None, imap(_dnf, where.children))
File "/home/harshil/workspace/seedinvest/local/lib/python2.7/site-packages/cacheops/utils.py", line 74, in _dnf
elif lookup == 'in' and len(value) < LONG_DISJUNCTION:
TypeError: object of type 'Query' has no len()

Cacheops crashes the entire application if for some reason it cannot connect to the configured Redis server

First of all, I really like how this library works, it's really great stuff!

However I did run into the problem described in the title.
This is dangerous behaviour on production, if your cache becomes unreachable for some reason then it will kill the entire application. It should instead continue along happily though obviously with degraded application performance. And ideally raising some visible warning that will alert admins.

With Django's core cache modules for example, if you kill the memcache machines that are configured to be used in settings, the application will keep working fine.

Example:

File "/Users/bberes/Sites/coveapi/django-cacheops/cacheops/query.py", line 411, in _post_save
invalidate_obj(instance)
File "/Users/bberes/Sites/coveapi/django-cacheops/cacheops/invalidation.py", line 163, in invalidate_obj
invalidate_from_dict(non_proxy(obj.class), obj.dict)
File "/Users/bberes/Sites/coveapi/django-cacheops/cacheops/invalidation.py", line 122, in invalidate_from_dict
schemes = cache_schemes.schemes(model)
File "/Users/bberes/Sites/coveapi/django-cacheops/cacheops/invalidation.py", line 61, in schemes
return self.load_schemes(model)
File "/Users/bberes/Sites/coveapi/django-cacheops/cacheops/invalidation.py", line 49, in load_schemes
version, members = txn.execute()
File "/Users/bberes/sites/env/coveapi/lib/python2.7/site-packages/redis/client.py", line 1821, in execute
return execute(conn, stack, raise_on_error)
File "/Users/bberes/sites/env/coveapi/lib/python2.7/site-packages/redis/client.py", line 1706, in _execute_transaction
connection.send_packed_command(all_cmds)
File "/Users/bberes/sites/env/coveapi/lib/python2.7/site-packages/redis/connection.py", line 283, in send_packed_command
self.connect()
File "/Users/bberes/sites/env/coveapi/lib/python2.7/site-packages/redis/connection.py", line 231, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error 61 connecting localhost:6379. Connection refused.

Exception with Q and subquery - Can't stringify django.db.models.sql.where.SubqueryConstraint

When there a query with Q and subquery cacheops fails with expection.
See code example below.

models

from django.contrib.auth.models import Group
from django.db import models

# Create your models here.
from django.db.models import TextField


class TM(models.Model):
     group = models.ForeignKey(Group, null=True, blank=True)

code

>>> from django.core.cache import cache
>>> from zxczxc.models import *
>>> from django.db.models import Q
>>> from django.contrib.auth.models import Group
>>> qs = TM.objects.filter(Q(group__in=Group.objects.all()) )
>>> cache.set("ttc5", qs)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\ShapeR\webadmenv\lib\site-packages\django\core\cache\backends\locmem.py", line 73, in set
    pickled = pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
  File "C:\Users\ShapeR\webadmenv\lib\site-packages\django\db\models\query.py", line 66, in __getstate__
    self._fetch_all()
  File "C:\Users\ShapeR\webadmenv\lib\site-packages\django\db\models\query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Users\ShapeR\webadmenv\lib\site-packages\django_cacheops-1.0.3-py2.7.egg\cacheops\query.py", line 346, in iterator
    cache_key = self._cache_key()
  File "C:\Users\ShapeR\webadmenv\lib\site-packages\django_cacheops-1.0.3-py2.7.egg\cacheops\query.py", line 254, in _cache_key
    md5.update(stringify_query(self.query))
  File "C:\Users\ShapeR\webadmenv\lib\site-packages\django_cacheops-1.0.3-py2.7.egg\cacheops\query.py", line 212, in stringify_query
    raise ValueError(*e.args)
ValueError: Can't stringify <django.db.models.sql.where.SubqueryConstraint object at 0x03369570>

ORDER BY and LIMIT/OFFSET caveats

Hi,

After some research, testing and code inspection of various Django model caching alternatives (mainly projects in http://www.djangopackages.com/grids/g/caching/), we've decided to go with cacheops.

Even it sometimes seems to be too aggressive invalidating data, even I'm not a fan of replicating same serialised instances in differente Redis keys, and even I would prefer a memcache-based solution using an LRU policy for discarding old/unused cached stuff (and therefore not setting random Redis timeouts in the cacheops cfg), cacheops seems to be the most complete and transparent approach at the moment.

After some testing it seem to work smoothly, but we have found problems with invalidations when editing elements in a simple listing of users paginated and ordered by some sort criteria. Disabling ordering seems to fix the issue. Reviewing the caveats section in the documentation it seems to be related to the 'ORDER BY and LIMIT/OFFSET don't affect invalidation' item... but, could you please provide some extra information about this limitation. It's not allowed to enable cacheops on ordered QuerySets?

Thanks! :)

Feature: Multi-server support

This would be also a very nice thing that I've been seriously looking at.
I think some options would be to integrate a smarter client wrapper instead of redis-py like:
https://github.com/gmr/mredis
https://github.com/salimane/rediscluster-py

Or just implement it directly, with the minimum required feature set for cacheops. I quite like how rediscluster works.
There are some obstacles however.

  1. The straightforward solution is to distribute all cache keys evenly based on the modulo of CRC32 hash. This is handled by the libs above. However the way cacheops works, I suppose the schemas would need to be always stored/updated on every (master) server.
  2. Cacheops uses pipelining in some places which is more tricky to update for multi-server. But should be doable, especially with rediscluster.

Any thoughts on this matter would be appreciated.

ImportError: No module named cacheops

After I install CacheOps and configure it, every request raises ImportError without breaking anything on site. Seems very odd that autodiscover() somehow involved.
CacheOps module is first in INSTALLED_APPS. Everything seems ok, but I suppose that if CACHEOPS have an “all” value for “.” key it might be the reason. I know that in real world it is stupid to do config like this, but it was a test and you may be interested in results.

Traceback (most recent call last):

 File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 101, in get_response
   request.path_info)

 File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 250, in resolve
   for pattern in self.url_patterns:

 File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 279, in _get_url_patterns
   patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

 File "/usr/local/lib/python2.6/dist-packages/django/core/urlresolvers.py", line 274, in _get_urlconf_module
   self._urlconf_module = import_module(self.urlconf_name)

 File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
   __import__(name)

 File "/var/www/launchnow/urls.py", line 9, in <module>
   admin.autodiscover()

 File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/__init__.py", line 22, in autodiscover
   mod = import_module(app)

 File "/usr/local/lib/python2.6/dist-packages/django/utils/importlib.py", line 35, in import_module
   __import__(name)

ImportError: No module named cacheops

Invalidation problem

Заметил такое странное поведение при инвалидации кэша.

  1. Стартую приложение
  2. Делаю запрос к вьюхе, в которой есть обращение к БД
    b = Brand.objects.get(slug='slug_key')
  3. redis-cli показывает:
    redis 127.0.0.1:6379[2]> smembers "schemes:catalog.brand"
    1. "slug"
    2. "id"
  4. Делаю flushdb
  5. Повторяю запрос п. 2
  6. Ключи для инвалидации не создаются
    redis 127.0.0.1:6379[2]> smembers "schemes:catalog.brand"
    (empty list or set)

Хотя в кэше они есть:

redis 127.0.0.1:6379[2]> keys brand

  1. "conj:catalog.brand:id=1"
  2. "conj:catalog.brand:slug=slug_key"

Что можно сделать, чтобы запросы по slug=*** проходили инвалидацию?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.