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