Skip to content

Commit

Permalink
Refactor caching
Browse files Browse the repository at this point in the history
  • Loading branch information
andbag committed Apr 3, 2018
1 parent 90ac2c0 commit e17ce28
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/Products/ZCatalog/Catalog.py
Expand Up @@ -29,7 +29,6 @@
from Missing import MV
from Persistence import Persistent
from ZTUtils.Lazy import LazyMap, LazyCat, LazyValues
from plone.memoize import ram

from Products.PluginIndexes.interfaces import (
ILimitedResultIndex,
Expand All @@ -39,7 +38,7 @@
from Products.PluginIndexes.util import safe_callable
from Products.ZCatalog.CatalogBrains import AbstractCatalogBrain, NoBrainer
from Products.ZCatalog.plan import CatalogPlan
from Products.ZCatalog.cache import _apply_query_plan_cachekey
from Products.ZCatalog.cache import cache
from Products.ZCatalog.ProgressHandler import ZLogHandler
from Products.ZCatalog.query import IndexQuery

Expand Down Expand Up @@ -596,9 +595,9 @@ def _search_index(self, cr, index_id, query, rs):

return rs

@ram.cache(_apply_query_plan_cachekey)
@cache()
def _apply_query_plan(self, cr, query):

plan = cr.plan()
if not plan:
plan = self._sorted_search_indexes(query)
Expand Down
15 changes: 10 additions & 5 deletions src/Products/ZCatalog/cache.py
Expand Up @@ -19,6 +19,7 @@
from Products.PluginIndexes.interfaces import IIndexCounter
from Products.PluginIndexes.interfaces import IDateRangeIndex
from Products.PluginIndexes.interfaces import IDateIndex
from plone.memoize import ram


class CatalogCacheKey(object):
Expand All @@ -45,7 +46,7 @@ def make_key(self, query):

catalog = self.catalog

key = []
keys = []
for name, value in query.items():
if name in catalog.indexes:
index = catalog.getIndex(name)
Expand All @@ -63,15 +64,15 @@ def make_key(self, query):
kvl = []
for k, v in value.items():
v = self._convert_datum(index, v)
v.append((k, v))
value = sorted(kvl)
kvl.append((k, v))
value = frozenset(kvl)

else:
value = self._convert_datum(index, value)

key.append((name, counter, value))
keys.append((name, counter, value))

key = tuple(sorted(key))
key = frozenset(keys)
cache_key = '%s-%s' % (self.cid, hash(key))
return cache_key

Expand Down Expand Up @@ -114,3 +115,7 @@ def _apply_query_plan_cachekey(method, catalog, plan, query):
if cc.key is None:
raise DontCache
return cc.key


def cache():
return ram.cache(_apply_query_plan_cachekey)

0 comments on commit e17ce28

Please sign in to comment.