Commit 3088f386 authored by Nicolas Delaby's avatar Nicolas Delaby

CachePlugins impliment ICachePlugin Interface

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@27013 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 1d692378
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
......@@ -33,6 +34,8 @@ from thread import get_ident
from zLOG import LOG
from BaseCache import BaseCache
from BaseCache import CacheEntry
from Products.ERP5Type import Interface
import zope.interface
try:
import memcache
......@@ -46,7 +49,11 @@ connection_pool = {}
class DistributedRamCache(BaseCache):
""" Memcached based cache plugin. """
def __init__(self, params):
zope.interface.implements(
Interface.ICachePlugin
)
def __init__(self, params={}):
self._servers = params.get('server', '')
self._debugLevel = params.get('debugLevel', 0)
BaseCache.__init__(self)
......@@ -56,7 +63,7 @@ class DistributedRamCache(BaseCache):
## cache storage is a memcached server and no need to init it
pass
def getCacheStorage(self):
def getCacheStorage(self, **kw):
## if we use one connection object this causes
## "MemCached: while expecting 'STORED', got unexpected response 'END'"
## messages in log files and can sometimes can block the thread.
......@@ -85,7 +92,7 @@ class DistributedRamCache(BaseCache):
cache_storage = self.getCacheStorage()
cache_id = self.checkAndFixCacheId(cache_id, scope)
cache_entry = cache_storage.get(cache_id)
#self.markCacheHit()
self.markCacheHit()
return cache_entry
def set(self, cache_id, scope, value, cache_duration= None, calculation_time=0):
......@@ -97,7 +104,7 @@ class DistributedRamCache(BaseCache):
cache_duration = 86400
cache_entry = CacheEntry(value, cache_duration, calculation_time)
cache_storage.set(cache_id, cache_entry, cache_duration)
#self.markCacheMiss()
self.markCacheMiss()
def expireOldCacheEntries(self, forceCheck = False):
""" Memcache has its own built in expire policy """
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
......@@ -32,6 +33,8 @@ Local RAM based cache plugin.
import time
from BaseCache import BaseCache, CacheEntry
from Products.ERP5Type import Interface
import zope.interface
def calcPythonObjectMemorySize(i):
""" Recursive function that will 'walk' over complex python types and caclulate
......@@ -49,6 +52,10 @@ def calcPythonObjectMemorySize(i):
class RamCache(BaseCache):
""" RAM based cache plugin."""
zope.interface.implements(
Interface.ICachePlugin
)
_cache_dict = {}
cache_expire_check_interval = 300
......@@ -60,7 +67,7 @@ class RamCache(BaseCache):
## cache storage is a RAM based dictionary
pass
def getCacheStorage(self):
def getCacheStorage(self, **kw):
return self._cache_dict
def get(self, cache_id, scope, default=None):
......
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
......@@ -35,6 +36,8 @@ import time
import base64
from zLOG import LOG
from BaseCache import BaseCache, CacheEntry, CachedMethodError
from Products.ERP5Type import Interface
import zope.interface
try:
import cPickle as pickle
......@@ -59,6 +62,10 @@ connection_pool = {}
class SQLCache(BaseCache):
""" SQL based cache plugin. """
zope.interface.implements(
Interface.ICachePlugin
)
cache_expire_check_interval = 3600
create_table_sql = '''CREATE TABLE %s(cache_id VARBINARY(970) NOT NULL,
......@@ -115,7 +122,7 @@ class SQLCache(BaseCache):
find_table_by_name_sql = """SHOW TABLES LIKE '%s' """
def __init__(self, params):
def __init__(self, params={}):
BaseCache.__init__(self)
self._dbConn = None
self._db_server = params.get('server', '')
......@@ -139,12 +146,13 @@ class SQLCache(BaseCache):
## no such table create it
self.execSQLQuery(self.create_table_sql %self._db_cache_table_name)
def getCacheStorage(self, force_reconnect=False):
def getCacheStorage(self, **kw):
"""
Return current DB connection or create a new one for this thread.
See http://sourceforge.net/docman/display_doc.php?docid=32071&group_id=22307
especially threadsafety part why we create for every thread a new MySQL db connection object.
"""
force_reconnect = kw.get('force_reconnect', False)
global connection_pool
thread_id = get_ident()
......
......@@ -33,12 +33,18 @@ ZODB Based cache plugin.
import time
from BaseCache import BaseCache, CacheEntry
from BTrees.OOBTree import OOBTree
from Products.ERP5Type import Interface
import zope.interface
PRIVATE_ATTRIBUTE_ZODB_CACHE_NAME = '_zodb_cache'
class ZODBCache(BaseCache):
""" ZODB based cache plugin."""
zope.interface.implements(
Interface.ICachePlugin
)
cache_tool = None
cache_expire_check_interval = 300
......@@ -55,7 +61,7 @@ class ZODBCache(BaseCache):
if getattr(self.cache_tool, PRIVATE_ATTRIBUTE_ZODB_CACHE_NAME, None) is None:
self.cache_tool._zodb_cache = OOBTree()
def getCacheStorage(self):
def getCacheStorage(self, **kw):
return getattr(self.cache_tool, PRIVATE_ATTRIBUTE_ZODB_CACHE_NAME)
def get(self, cache_id, scope, default=None):
......
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