From f8b0a53c3f3e4a24a464db239640add018343099 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 7 Sep 2013 10:31:39 +0200 Subject: [PATCH] cache is always a dictionary in memory --- test/test_storage.py | 2 + tiramisu/config.py | 6 +- tiramisu/storage/__init__.py | 3 +- tiramisu/storage/{dictionary => }/cache.py | 0 tiramisu/storage/dictionary/setting.py | 2 +- tiramisu/storage/dictionary/value.py | 2 +- tiramisu/storage/sqlite3/cache.py | 98 ---------------------- tiramisu/storage/sqlite3/setting.py | 6 +- tiramisu/storage/sqlite3/sqlite3db.py | 44 ++++++++++ tiramisu/storage/sqlite3/value.py | 6 +- 10 files changed, 59 insertions(+), 110 deletions(-) rename tiramisu/storage/{dictionary => }/cache.py (100%) delete mode 100644 tiramisu/storage/sqlite3/cache.py create mode 100644 tiramisu/storage/sqlite3/sqlite3db.py diff --git a/test/test_storage.py b/test/test_storage.py index 1527f5f..c3acc70 100644 --- a/test/test_storage.py +++ b/test/test_storage.py @@ -6,7 +6,9 @@ from tiramisu.config import Config from tiramisu.option import BoolOption, OptionDescription from tiramisu.setting import owners from tiramisu.storage import list_sessions, delete_session +from tiramisu import setting +setting.expires_time = 0 def test_non_persistent(): b = BoolOption('b', '') diff --git a/tiramisu/config.py b/tiramisu/config.py index 4659b51..fac3caf 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -24,7 +24,7 @@ import weakref from tiramisu.error import PropertiesOptionError, ConfigError from tiramisu.option import OptionDescription, Option, SymLinkOption from tiramisu.setting import groups, Settings, default_encoding -from tiramisu.storage import get_storage +from tiramisu.storage import get_storages from tiramisu.value import Values from tiramisu.i18n import _ @@ -536,7 +536,7 @@ class Config(CommonConfig): :param persistent: if persistent, don't delete storage when leaving :type persistent: `boolean` """ - settings, values = get_storage(self, session_id, persistent) + settings, values = get_storages(self, session_id, persistent) self._impl_settings = Settings(self, settings) self._impl_values = Values(self, values) super(Config, self).__init__(descr, weakref.ref(self)) @@ -576,7 +576,7 @@ class Config(CommonConfig): # child._impl_meta = self # self._impl_children = children -# settings, values = get_storage(self, session_id, persistent) +# settings, values = get_storages(self, session_id, persistent) # self._impl_settings = Settings(self, settings) # self._impl_values = Values(self, values) # self._impl_meta = None diff --git a/tiramisu/storage/__init__.py b/tiramisu/storage/__init__.py index ee74c16..562aad5 100644 --- a/tiramisu/storage/__init__.py +++ b/tiramisu/storage/__init__.py @@ -81,7 +81,7 @@ def set_storage(name, **args): '').format(option, name)) -def get_storage(context, session_id, persistent): +def get_storages(context, session_id, persistent): def gen_id(config): return str(id(config)) + str(time()) @@ -101,6 +101,7 @@ def list_sessions(): def delete_session(session_id): """Delete a selected session, be careful, you can deleted a session use by an other instance + :params session_id: id of session to delete """ return storage_type.get().delete_session(session_id) diff --git a/tiramisu/storage/dictionary/cache.py b/tiramisu/storage/cache.py similarity index 100% rename from tiramisu/storage/dictionary/cache.py rename to tiramisu/storage/cache.py diff --git a/tiramisu/storage/dictionary/setting.py b/tiramisu/storage/dictionary/setting.py index ea59513..706ab2a 100644 --- a/tiramisu/storage/dictionary/setting.py +++ b/tiramisu/storage/dictionary/setting.py @@ -17,7 +17,7 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # ____________________________________________________________ -from .cache import Cache +from ..cache import Cache class Settings(Cache): diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index ed1017e..c435d06 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -18,7 +18,7 @@ # # ____________________________________________________________ -from .cache import Cache +from ..cache import Cache class Values(Cache): diff --git a/tiramisu/storage/sqlite3/cache.py b/tiramisu/storage/sqlite3/cache.py deleted file mode 100644 index 183c4d0..0000000 --- a/tiramisu/storage/sqlite3/cache.py +++ /dev/null @@ -1,98 +0,0 @@ -# -*- coding: utf-8 -*- -"sqlite3 cache" -# 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 pickle import dumps, loads - - -class Cache(object): - __slots__ = ('storage',) - key_is_path = True - - def __init__(self, cache_type, storage): - self.storage = storage - cache_table = 'CREATE TABLE IF NOT EXISTS cache_{0}(path '.format( - cache_type) - cache_table += 'text primary key, value text, time real)' - self.storage.execute(cache_table) - - # value - def _sqlite_decode_path(self, path): - if path == '_none': - return None - else: - return path - - def _sqlite_encode_path(self, path): - if path is None: - return '_none' - else: - return path - - def _sqlite_decode(self, value): - return loads(value) - - def _sqlite_encode(self, value): - if isinstance(value, list): - value = list(value) - return dumps(value) - - def setcache(self, cache_type, path, val, time): - convert_value = self._sqlite_encode(val) - path = self._sqlite_encode_path(path) - self.storage.execute("DELETE FROM cache_{0} WHERE path = ?".format( - cache_type), (path,), False) - self.storage.execute("INSERT INTO cache_{0}(path, value, time) " - "VALUES (?, ?, ?)".format(cache_type), - (path, convert_value, time)) - - def getcache(self, cache_type, path, exp): - path = self._sqlite_encode_path(path) - cached = self.storage.select("SELECT value FROM cache_{0} WHERE " - "path = ? AND time >= ?".format( - cache_type), (path, exp)) - if cached is None: - return False, None - else: - return True, self._sqlite_decode(cached[0]) - - def hascache(self, cache_type, path): - path = self._sqlite_encode_path(path) - return self.storage.select("SELECT value FROM cache_{0} WHERE " - "path = ?".format(cache_type), - (path,)) is not None - - def reset_expired_cache(self, cache_type, exp): - self.storage.execute("DELETE FROM cache_{0} WHERE time < ?".format( - cache_type), (exp,)) - - def reset_all_cache(self, cache_type): - self.storage.execute("DELETE FROM cache_{0}".format(cache_type)) - - def get_cached(self, cache_type, context): - """return all values in a dictionary - example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')} - """ - ret = {} - for path, value, time in self.storage.select("SELECT * FROM cache_{0}" - "".format(cache_type), - only_one=False): - path = self._sqlite_decode_path(path) - value = self._sqlite_decode(value) - ret[path] = (value, time) - return ret diff --git a/tiramisu/storage/sqlite3/setting.py b/tiramisu/storage/sqlite3/setting.py index 11506bb..720849b 100644 --- a/tiramisu/storage/sqlite3/setting.py +++ b/tiramisu/storage/sqlite3/setting.py @@ -17,10 +17,10 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # ____________________________________________________________ -from .cache import Cache +from .sqlite3db import Sqlite3DB -class Settings(Cache): +class Settings(Sqlite3DB): __slots__ = tuple() def __init__(self, storage): @@ -29,7 +29,7 @@ class Settings(Cache): permissives_table = 'CREATE TABLE IF NOT EXISTS permissive(path text ' permissives_table += 'primary key, permissives text)' # should init cache too - super(Settings, self).__init__('property', storage) + super(Settings, self).__init__(storage) self.storage.execute(settings_table, commit=False) self.storage.execute(permissives_table) diff --git a/tiramisu/storage/sqlite3/sqlite3db.py b/tiramisu/storage/sqlite3/sqlite3db.py new file mode 100644 index 0000000..9a967cd --- /dev/null +++ b/tiramisu/storage/sqlite3/sqlite3db.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +"sqlite3 cache" +# 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 cPickle import loads, dumps +from ..cache import Cache + + +class Sqlite3DB(Cache): + __slots__ = tuple() + def _sqlite_decode_path(self, path): + if path == '_none': + return None + else: + return path + + def _sqlite_encode_path(self, path): + if path is None: + return '_none' + else: + return path + + def _sqlite_decode(self, value): + return loads(value) + + def _sqlite_encode(self, value): + if isinstance(value, list): + value = list(value) + return dumps(value) diff --git a/tiramisu/storage/sqlite3/value.py b/tiramisu/storage/sqlite3/value.py index 687e7dd..3f76e2c 100644 --- a/tiramisu/storage/sqlite3/value.py +++ b/tiramisu/storage/sqlite3/value.py @@ -18,18 +18,18 @@ # # ____________________________________________________________ -from .cache import Cache +from .sqlite3db import Sqlite3DB from tiramisu.setting import owners -class Values(Cache): +class Values(Sqlite3DB): __slots__ = ('__weakref__',) def __init__(self, storage): """init plugin means create values storage """ # should init cache too - super(Values, self).__init__('value', storage) + super(Values, self).__init__(storage) values_table = 'CREATE TABLE IF NOT EXISTS value(path text primary ' values_table += 'key, value text, owner text)' self.storage.execute(values_table, commit=False)