Commit ec47b8c8 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Use a single dict with tuple keys, rather than a dict of dicts.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18350 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2b7b8dbb
...@@ -32,7 +32,6 @@ Local RAM based cache plugin. ...@@ -32,7 +32,6 @@ Local RAM based cache plugin.
import time import time
from Products.ERP5Type.Cache import DEFAULT_CACHE_SCOPE
from BaseCache import BaseCache from BaseCache import BaseCache
from BaseCache import CacheEntry from BaseCache import CacheEntry
...@@ -69,7 +68,7 @@ class RamCache(BaseCache): ...@@ -69,7 +68,7 @@ class RamCache(BaseCache):
def get(self, cache_id, scope, default=None): def get(self, cache_id, scope, default=None):
cache = self.getCacheStorage() cache = self.getCacheStorage()
try: try:
cache_entry = cache[scope][cache_id] cache_entry = cache[(scope, cache_id)]
cache_entry.markCacheHit() cache_entry.markCacheHit()
self.markCacheHit() self.markCacheHit()
return cache_entry return cache_entry
...@@ -79,8 +78,7 @@ class RamCache(BaseCache): ...@@ -79,8 +78,7 @@ class RamCache(BaseCache):
def set(self, cache_id, scope, value, cache_duration=None, calculation_time=0): def set(self, cache_id, scope, value, cache_duration=None, calculation_time=0):
cache = self.getCacheStorage() cache = self.getCacheStorage()
slot = cache.setdefault(scope, {}) cache[(scope, cache_id)] = CacheEntry(value, cache_duration, calculation_time)
slot[cache_id] = CacheEntry(value, cache_duration, calculation_time)
self.markCacheMiss() self.markCacheMiss()
def expireOldCacheEntries(self, forceCheck = False): def expireOldCacheEntries(self, forceCheck = False):
...@@ -89,44 +87,50 @@ class RamCache(BaseCache): ...@@ -89,44 +87,50 @@ class RamCache(BaseCache):
## time to check for expired cache items ## time to check for expired cache items
self._last_cache_expire_check_at = now self._last_cache_expire_check_at = now
cache = self.getCacheStorage() cache = self.getCacheStorage()
for scope in cache.keys(): for key, value in cache.items():
for (cache_id, cache_item) in cache[scope].items(): if value.isExpired():
if cache_item.isExpired()==True: try:
del cache[scope][cache_id] del cache[key]
except KeyError:
# The key might have disappeared, due to multi-threading.
pass
def delete(self, cache_id, scope): def delete(self, cache_id, scope):
try: try:
del self.getCacheStorage()[scope][cache_id] del self.getCacheStorage()[(scope, cache_id)]
except KeyError: except KeyError:
pass pass
def has_key(self, cache_id, scope): def has_key(self, cache_id, scope):
cache = self.getCacheStorage() cache = self.getCacheStorage()
slot = cache.setdefault(scope, {}) return cache.has_key((scope, cache_id))
return slot.has_key(cache_id)
def getScopeList(self): def getScopeList(self):
scope_list = [] scope_set = set()
## some cache scopes in RAM Cache can have no cache_ids keys but for scope, cache_id in self.getCacheStorage().iterkeys():
## they do exists. To have consistent behaviour with SQLCache plugin scope_set.add(scope)
## where cache scope will not exists without its cache_ids we filter them. return list(scope_set)
for scope, item in self.getCacheStorage().items():
if item!={}:
scope_list.append(scope)
return scope_list
def getScopeKeyList(self, scope): def getScopeKeyList(self, scope):
return self.getCacheStorage()[scope].keys() key_list = []
for key in self.getCacheStorage().iterkeys():
if scope == key[0]:
key_list.append(key[1])
return key_list
def clearCache(self): def clearCache(self):
BaseCache.clearCache(self) BaseCache.clearCache(self)
self._cache_dict = {DEFAULT_CACHE_SCOPE: {}} self.getCacheStorage().clear()
def clearCacheForScope(self, scope): def clearCacheForScope(self, scope):
try: cache = self.getCacheStorage()
self.getCacheStorage()[scope] = {} for key in cache.keys():
except KeyError: if key[0] == scope:
pass try:
del cache[key]
except KeyError:
# The key might have disappeared, due to multi-threading.
pass
def getCachePluginTotalMemorySize(self): def getCachePluginTotalMemorySize(self):
""" Calculate total RAM memory size of cache plugin. """ Calculate total RAM memory size of cache plugin.
...@@ -135,8 +139,9 @@ class RamCache(BaseCache): ...@@ -135,8 +139,9 @@ class RamCache(BaseCache):
""" """
total_size = 0 total_size = 0
cache_keys_total_size = {} cache_keys_total_size = {}
for cache_key, cache_value in self._cache_dict[DEFAULT_CACHE_SCOPE].items(): cache = self.getCacheStorage()
cache_item_size = calcPythonObjectMemorySize(cache_value.getValue()) for key, value in cache.iteritems():
total_size += cache_item_size value_size = calcPythonObjectMemorySize(value)
cache_keys_total_size[cache_key] = cache_item_size total_size += value_size
cache_keys_total_size[key[1]] = value_size
return total_size, cache_keys_total_size return total_size, cache_keys_total_size
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment