{"action":"created","issue":{"url":"https://api.github.com/repos/BertrandBordage/django-cachalot/issues/21","labels_url":"https://api.github.com/repos/BertrandBordage/django-cachalot/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/BertrandBordage/django-cachalot/issues/21/comments","events_url":"https://api.github.com/repos/BertrandBordage/django-cachalot/issues/21/events","html_url":"https://github.com/BertrandBordage/django-cachalot/issues/21","id":88785398,"number":21,"title":"Potential bug when using django.contrib.gis","user":{"login":"ebrelsford","id":375002,"avatar_url":"https://avatars.githubusercontent.com/u/375002?v=3","gravatar_id":"","url":"https://api.github.com/users/ebrelsford","html_url":"https://github.com/ebrelsford","followers_url":"https://api.github.com/users/ebrelsford/followers","following_url":"https://api.github.com/users/ebrelsford/following{/other_user}","gists_url":"https://api.github.com/users/ebrelsford/gists{/gist_id}","starred_url":"https://api.github.com/users/ebrelsford/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebrelsford/subscriptions","organizations_url":"https://api.github.com/users/ebrelsford/orgs","repos_url":"https://api.github.com/users/ebrelsford/repos","events_url":"https://api.github.com/users/ebrelsford/events{/privacy}","received_events_url":"https://api.github.com/users/ebrelsford/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-16T17:11:28Z","updated_at":"2015-06-16T18:04:47Z","closed_at":null,"body":"Apologies for the vagueness here, I've looked at this a bit and plan on coming back to it, but I thought I'd write this down in case you have any ideas.\r\n\r\n[These lines](https://github.com/596acres/nycdata/blob/master/nycdata/nycha/views.py#L33-L36) in my Django site return inconsistent and incorrect results. They count the number of model instances within a geometry represented in another instance. I'm relatively sure the issue has to do with `django-cachalot` since [invalidating the model cache before each query](https://github.com/596acres/nycdata/blob/master/nycdata/nycha/views.py#L22-L28) fixes the problem.\r\n\r\nEach time the sql is the same, and the parameters will be something like this:\r\n\r\n    ('*', 41, True, True, 'private', True, 3, <django.contrib.gis.db.backends.postgis.adapter.PostGISAdapter object at 0x7fcd2a807790>, True)\r\n\r\nMy hunch right now is that the `PostGISAdapter` gets SHAed to the same value sometimes as it is reused even for different geometries, so the cache key is the same for multiple queries that use different geometries.\r\n\r\nPlease let me know if there's more info I can provide, and what you might suggest I look at next."},"comment":{"url":"https://api.github.com/repos/BertrandBordage/django-cachalot/issues/comments/112516127","html_url":"https://github.com/BertrandBordage/django-cachalot/issues/21#issuecomment-112516127","issue_url":"https://api.github.com/repos/BertrandBordage/django-cachalot/issues/21","id":112516127,"user":{"login":"BertrandBordage","id":1119169,"avatar_url":"https://avatars.githubusercontent.com/u/1119169?v=3","gravatar_id":"","url":"https://api.github.com/users/BertrandBordage","html_url":"https://github.com/BertrandBordage","followers_url":"https://api.github.com/users/BertrandBordage/followers","following_url":"https://api.github.com/users/BertrandBordage/following{/other_user}","gists_url":"https://api.github.com/users/BertrandBordage/gists{/gist_id}","starred_url":"https://api.github.com/users/BertrandBordage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BertrandBordage/subscriptions","organizations_url":"https://api.github.com/users/BertrandBordage/orgs","repos_url":"https://api.github.com/users/BertrandBordage/repos","events_url":"https://api.github.com/users/BertrandBordage/events{/privacy}","received_events_url":"https://api.github.com/users/BertrandBordage/received_events","type":"User","site_admin":false},"created_at":"2015-06-16T18:04:47Z","updated_at":"2015-06-16T18:04:47Z","body":"Indeed, that’s an invalidation issue, and I suppose you’re right about its cause.\r\n\r\nCould you try with a custom cache key generator?\r\nDefine this function in a file:\r\n```python\r\ndef get_query_cache_key(compiler):\r\n    sql, params = compiler.as_sql()\r\n    cache_key = '%s:%s:%s' % (compiler.using, sql, [(hash(p), type(p)) for p in params])\r\n    return sha1(cache_key.encode('utf-8')).hexdigest()\r\n```\r\nAnd tell Cachalot to use it by setting `CACHALOT_QUERY_KEYGEN = 'path.to.your.get_query_cache_key'`"}}