tiramisu/tiramisu/storage/dictionary/storage.py

83 lines
2.4 KiB
Python
Raw Normal View History

2013-08-14 23:06:31 +02:00
# -*- 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-08-20 22:45:11 +02:00
from tiramisu.i18n import _
from tiramisu.error import ConfigError
def enumerate():
return []
def delete(session_id):
raise ConfigError(_('dictionary storage cannot delete session'))
2013-08-20 22:45:11 +02:00
class Storage(object):
__slots__ = tuple()
storage = 'dictionary'
def __init__(self, session_id, is_persistent):
2013-08-20 22:45:11 +02:00
if is_persistent:
raise ValueError(_('a dictionary cannot be persistent'))
2013-08-20 09:47:12 +02:00
class Cache(object):
2013-08-14 23:06:31 +02:00
__slots__ = ('_cache',)
2013-08-20 09:47:12 +02:00
key_is_path = False
2013-08-14 23:06:31 +02:00
def __init__(self):
self._cache = {}
def setcache(self, cache_type, path, val, time):
self._cache[path] = (val, time)
2013-08-14 23:06:31 +02:00
def getcache(self, cache_type, path, exp):
value, created = self._cache[path]
2013-08-14 23:06:31 +02:00
if exp < created:
return True, value
return False, None
def hascache(self, cache_type, path):
""" path is in the cache
2013-08-21 17:21:09 +02:00
:param cache_type: value | property
:param path: the path's option
2013-08-21 17:21:09 +02:00
"""
return path in self._cache
2013-08-14 23:06:31 +02:00
2013-08-20 09:47:12 +02:00
def reset_expired_cache(self, cache_type, exp):
2013-08-14 23:06:31 +02:00
keys = self._cache.keys()
for key in keys:
val, created = self._cache[key]
if exp > created:
del(self._cache[key])
2013-08-20 09:47:12 +02:00
def reset_all_cache(self, cache_type):
2013-08-21 17:21:09 +02:00
"empty the cache"
2013-08-14 23:06:31 +02:00
self._cache.clear()
2013-08-19 11:01:21 +02:00
2013-08-20 09:47:12 +02:00
def get_cached(self, cache_type, context):
2013-08-19 11:01:21 +02:00
"""return all values in a dictionary
example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
2013-08-19 11:01:21 +02:00
"""
return self._cache