Skip to content

Commit

Permalink
Add ModelCacheClass.connect
Browse files Browse the repository at this point in the history
  • Loading branch information
dchentech committed Aug 1, 2014
1 parent c654bf5 commit 4957ddb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
12 changes: 1 addition & 11 deletions model_cache/model_cache.py
Expand Up @@ -41,17 +41,7 @@ class OriginalClass(): pass # so we can setattr here.
_model_cache.__name__ = decorated_class.__name__
_model_cache.__module__ = decorated_class.__module__ # so can pickle :)

dbpath = None
if _model_cache.original.cache_dir:
dbpath = os.path.join(_model_cache.original.cache_dir, \
repr(_model_cache).split("'")[1].split(".")[-1] + ".db")

_model_cache.datadict = {
"memory" : ModelCacheStoreMemory,
"sqlite" : ModelCacheStoreSqlite,
"redis" : ModelCacheStoreRedis,
}[_model_cache.original.storage_type](dbpath)
print "[ModelCache] Init at %s" % (dbpath or '[memory]')
_model_cache.connect()

return _model_cache
return _model_cache_decorator
19 changes: 19 additions & 0 deletions model_cache/model_cache_class.py
Expand Up @@ -42,6 +42,25 @@ def has_item_id(self, record):
""" Detect if there is an item_id, which should be already wrote to database """
raise NotImplemented

@classmethod
def connect(cls): cls.reconnect(is_reconnect=False)

@classmethod
def reconnect(cls, is_reconnect=True):
dbpath = None
if cls.original.cache_dir:
dbpath = os.path.join(cls.original.cache_dir, \
repr(cls).split("'")[1].split(".")[-1] + ".db")

cls.datadict = {
"memory" : ModelCacheStoreMemory,
"sqlite" : ModelCacheStoreSqlite,
"redis" : ModelCacheStoreRedis,
}[cls.original.storage_type](dbpath)

msg = 'Reconnect' if is_reconnect else 'Init'
print "[ModelCache] %s at %s" % (msg, dbpath or '[memory]')

@classmethod
def build_indexes(cls, items=[]):
# items 必定是list, 经过cPickle反序列化回来的
Expand Down

0 comments on commit 4957ddb

Please sign in to comment.