1 # -*- coding: utf-8 -*-
2 "default plugin for cache: set it in a simple dictionary"
3 # Copyright (C) 2013 Team tiramisu (see AUTHORS for all contributors)
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # ____________________________________________________________
21 from pickle import dumps, loads
23 from os.path import basename, splitext, join
32 def _gen_filename(name):
33 return join(dir_database, '{0}.{1}'.format(name, extension))
38 for filename in glob(_gen_filename('*')):
39 names.append(basename(splitext(filename)[0]))
43 def delete(session_id):
44 unlink(_gen_filename(session_id))
47 class Storage(object):
48 __slots__ = ('_conn', '_cursor', 'is_persistent', '_session_id')
51 def __init__(self, session_id, is_persistent):
52 self.is_persistent = is_persistent
53 self._session_id = session_id
54 self._conn = sqlite3.connect(_gen_filename(self._session_id))
55 self._conn.text_factory = str
56 self._cursor = self._conn.cursor()
58 def execute(self, sql, params=None, commit=True):
61 self._cursor.execute(sql, params)
65 def select(self, sql, params=None, only_one=True):
66 self.execute(sql, params=params, commit=False)
68 return self._cursor.fetchone()
70 return self._cursor.fetchall()
75 if not self.is_persistent:
76 delete(self._session_id)
80 __slots__ = ('storage',)
83 def __init__(self, cache_type, storage):
84 self.storage = storage
85 cache_table = 'CREATE TABLE IF NOT EXISTS cache_{0}(path '.format(
87 cache_table += 'text primary key, value text, time real)'
88 self.storage.execute(cache_table)
91 def _sqlite_decode_path(self, path):
97 def _sqlite_encode_path(self, path):
103 def _sqlite_decode(self, value):
106 def _sqlite_encode(self, value):
107 if isinstance(value, list):
111 def setcache(self, cache_type, path, val, time):
112 convert_value = self._sqlite_encode(val)
113 path = self._sqlite_encode_path(path)
114 self.storage.execute("DELETE FROM cache_{0} WHERE path = ?".format(
115 cache_type), (path,), False)
116 self.storage.execute("INSERT INTO cache_{0}(path, value, time) "
117 "VALUES (?, ?, ?)".format(cache_type),
118 (path, convert_value, time))
120 def getcache(self, cache_type, path, exp):
121 path = self._sqlite_encode_path(path)
122 cached = self.storage.select("SELECT value FROM cache_{0} WHERE "
123 "path = ? AND time >= ?".format(
124 cache_type), (path, exp))
128 return True, self._sqlite_decode(cached[0])
130 def hascache(self, cache_type, path):
131 path = self._sqlite_encode_path(path)
132 return self.storage.select("SELECT value FROM cache_{0} WHERE "
133 "path = ?".format(cache_type),
136 def reset_expired_cache(self, cache_type, exp):
137 self.storage.execute("DELETE FROM cache_{0} WHERE time < ?".format(
140 def reset_all_cache(self, cache_type):
141 self.storage.execute("DELETE FROM cache_{0}".format(cache_type))
143 def get_cached(self, cache_type, context):
144 """return all values in a dictionary
145 example: {'path1': ('value1', 'time1'), 'path2': ('value2', 'time2')}
148 for path, value, time in self.storage.select("SELECT * FROM cache_{0}"
149 "".format(cache_type),
151 path = self._sqlite_decode_path(path)
152 value = self._sqlite_decode(value)
153 ret[path] = (value, time)