82 lines
3.3 KiB
Python
82 lines
3.3 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 pickle import dumps, loads
|
|
import sqlite3
|
|
|
|
|
|
class Cache(object):
|
|
__slots__ = ('_conn', '_cursor')
|
|
key_is_path = True
|
|
|
|
def __init__(self, config_id, cache_type):
|
|
cache_table = 'CREATE TABLE IF NOT EXISTS cache_{0}(path text primary key, value text, time real)'.format(cache_type)
|
|
self._conn = sqlite3.connect(config_id + '.db')
|
|
self._conn.text_factory = str
|
|
self._cursor = self._conn.cursor()
|
|
self._cursor.execute(cache_table)
|
|
|
|
# value
|
|
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)
|
|
self._cursor.execute("DELETE FROM cache_{0} WHERE path = ?".format(cache_type), (path,))
|
|
self._cursor.execute("INSERT INTO cache_{0}(path, value, time) VALUES (?, ?, ?)".format(cache_type),
|
|
(path, convert_value, time))
|
|
self._conn.commit()
|
|
|
|
def getcache(self, cache_type, path, exp):
|
|
self._cursor.execute("SELECT value FROM cache_{0} WHERE path = ? AND time >= ?".format(cache_type), (path, exp))
|
|
cached = self._cursor.fetchone()
|
|
if cached is None:
|
|
return False, None
|
|
else:
|
|
return True, self._sqlite_decode(cached[0])
|
|
|
|
def hascache(self, cache_type, path):
|
|
self._cursor.execute("SELECT value FROM cache_{0} WHERE path = ?".format(cache_type), (path,))
|
|
return self._cursor.fetchone() is not None
|
|
|
|
def reset_expired_cache(self, cache_type, exp):
|
|
self._cursor.execute("DELETE FROM cache_{0} WHERE time < ?".format(cache_type), (exp,))
|
|
self._conn.commit()
|
|
|
|
def reset_all_cache(self, cache_type):
|
|
self._cursor.execute("DELETE FROM cache_{0}".format(cache_type))
|
|
self._conn.commit()
|
|
|
|
def get_cached(self, cache_type, context):
|
|
"""return all values in a dictionary
|
|
example: {option1: ('value1', 'time1'), option2: ('value2', 'time2')}
|
|
"""
|
|
self._cursor.execute("SELECT * FROM cache_{0}".format(cache_type))
|
|
ret = {}
|
|
for path, value, time in self._cursor.fetchall():
|
|
opt = context.cfgimpl_get_description().impl_get_opt_by_path(path)
|
|
value = self._sqlite_decode(value)
|
|
ret[opt] = (value, time)
|
|
return ret
|