tiramisu/tiramisu/storage/util.py

111 lines
3.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"default plugin for cache: set it in a simple dictionary"
# Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ____________________________________________________________
2013-09-22 20:57:52 +02:00
from tiramisu.setting import owners
class SerializeObject(object):
def __getstate__(self):
ret = {}
for key in dir(self):
if not key.startswith('__'):
ret[key] = getattr(self, key)
return ret
class Cache(object):
2013-09-22 20:57:52 +02:00
__slots__ = ('_cache', '_storage')
key_is_path = False
def __init__(self, storage):
self._cache = {}
2013-09-22 20:57:52 +02:00
self._storage = storage
def __getstate__(self):
slots = set()
for subclass in self.__class__.__mro__:
if subclass is not object:
slots.update(subclass.__slots__)
slots -= frozenset(['__weakref__', '_storage'])
states = {}
for slot in slots:
try:
value = getattr(self, slot)
#value has owners object, need 'str()' it
if slot == '_values':
_value = {}
for key, values in value.items():
vals = list(values)
vals[0] = str(vals[0])
_value[key] = tuple(vals)
states[slot] = _value
else:
states[slot] = value
except AttributeError:
pass
return states
def __setstate__(self, states):
for key, value in states.items():
#value has owners object, need to reconstruct it
if key == '_values':
_value = {}
for key_, values_ in value.items():
vals = list(values_)
try:
vals[0] = getattr(owners, vals[0])
except AttributeError:
owners.addowner(vals[0])
vals[0] = getattr(owners, vals[0])
_value[key_] = tuple(vals)
value = _value
setattr(self, key, value)
2013-09-07 17:25:22 +02:00
def setcache(self, path, val, time):
self._cache[path] = (val, time)
2013-09-07 17:25:22 +02:00
def getcache(self, path, exp):
value, created = self._cache[path]
2013-09-07 22:37:13 +02:00
if created is None or exp <= created:
return True, value
return False, None
2013-09-07 17:25:22 +02:00
def hascache(self, path):
""" path is in the cache
:param path: the path's option
"""
return path in self._cache
2013-09-07 17:25:22 +02:00
def reset_expired_cache(self, exp):
for key in tuple(self._cache.keys()):
val, created = self._cache[key]
2013-09-07 17:25:22 +02:00
if created is not None and exp > created:
del(self._cache[key])
2013-09-07 17:25:22 +02:00
def reset_all_cache(self):
"empty the cache"
self._cache.clear()
2013-09-07 17:25:22 +02:00
def get_cached(self, context):
"""return all values in a dictionary
example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
"""
return self._cache