Commit 4fb8f829 authored by Nicolas Delaby's avatar Nicolas Delaby

Calculate Cache value size based on string len

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@27177 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent f49ef6fe
......@@ -30,12 +30,13 @@
"""
Base Cache plugin.
"""
import time
class CachedMethodError(Exception):
pass
ACTIVATE_TRACKING = False
class CacheEntry(object):
""" Cachable entry. Used as a wrapper around real values stored in cache.
value
......@@ -60,13 +61,35 @@ class CacheEntry(object):
def markCacheHit(self, delta=1):
""" mark a read to this cache entry """
self._cache_hit_count = self._cache_hit_count + delta
if ACTIVATE_TRACKING:
self._cache_hit_count = self._cache_hit_count + delta
def getValue(self):
""" return cached value """
return getattr(self, 'value', None)
ACTIVATE_TRACKING = False
def __len__(self):
"""return value size
"""
value = self.getValue()
def calculateSize(v):
size = 0
if isinstance(v, (tuple, list)):
for item in v:
size += calculateSize(item)
elif isinstance(v, str):
size += len(v)
else:
#try to convert into string
try:
size += calculateSize(str(v))
except UnicodeEncodeError: #Maybe other exceptions should be handled
pass
return size
if value is None:
return 0
return calculateSize(value)
class BaseCache(object):
""" Base Cache class """
......
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