65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
# -*- 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
|
|
#
|
|
# ____________________________________________________________
|
|
|
|
|
|
from tiramisu.i18n import _
|
|
|
|
|
|
class Storage(object):
|
|
__slots__ = tuple()
|
|
def __init__(self, config_id, is_persistent):
|
|
if is_persistent:
|
|
raise ValueError(_('a dictionary cannot be persistent'))
|
|
|
|
|
|
class Cache(object):
|
|
__slots__ = ('_cache',)
|
|
key_is_path = False
|
|
|
|
def __init__(self):
|
|
self._cache = {}
|
|
|
|
def setcache(self, cache_type, opt, val, time):
|
|
self._cache[opt] = (val, time)
|
|
|
|
def getcache(self, cache_type, opt, exp):
|
|
value, created = self._cache[opt]
|
|
if exp < created:
|
|
return True, value
|
|
return False, None
|
|
|
|
def hascache(self, cache_type, opt):
|
|
return opt in self._cache
|
|
|
|
def reset_expired_cache(self, cache_type, exp):
|
|
keys = self._cache.keys()
|
|
for key in keys:
|
|
val, created = self._cache[key]
|
|
if exp > created:
|
|
del(self._cache[key])
|
|
|
|
def reset_all_cache(self, cache_type):
|
|
self._cache.clear()
|
|
|
|
def get_cached(self, cache_type, context):
|
|
"""return all values in a dictionary
|
|
example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')}
|
|
"""
|
|
return self._cache
|