core (memory/cache): file caching handling, first draft:

instead of having file caching handled individually by plugins, a generic module has been added in memory.

    - Cache can be global or associated to a profile. In the later case, client.cache can be used.
    - Cache are managed with unique ids (which can be any unique unicode, hash uuid, or something else).
    - To know if a file is in cache, getFilePath is used: if the file is in cache, its absolute path is returned, else None is returned.
    - To cache a file, cacheData is used with at list the source of cache (most of time plugin import name), and unique id. The method return file opened in binary writing mode (so cacheData can - and should - be used with "with" statement).
    - 2 files will be created: a metadata file (named after the unique id), and the actual file.
    - each file has a end of life time, after it, the cache is invalidated and the file must be requested again.
This commit is contained in:
Goffi 2017-01-05 20:23:38 +01:00
commit 6872df4fd3
2 changed files with 130 additions and 0 deletions

View file

@ -132,6 +132,9 @@ class Const(object):
NS_DELAY = 'urn:xmpp:delay'
NS_XHTML = 'http://www.w3.org/1999/xhtml'
## Directories ##
CACHE_DIR = u'cache'
## Configuration ##
if BaseDirectory: # skipped when xdg module is not available (should not happen in backend)
@ -281,6 +284,7 @@ class Const(object):
ENV_PREFIX = 'SAT_' # Prefix used for environment variables
IGNORE = 'ignore'
NO_LIMIT = -1 # used in bridge when a integer value is expected
DEFAULT_MAX_AGE = 1209600 # default max age of cached files, in seconds
## ANSI escape sequences ##

View file

@ -19,6 +19,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.memory import cache
from twisted.internet import task, defer
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
from twisted.words.protocols.jabber import xmlstream
@ -60,6 +61,7 @@ class SatXMPPClient(wokkel_client.XMPPClient):
self.__connected = False
self.profile = profile
self.host_app = host_app
self.cache = cache.Cache(host_app, profile)
self._mess_id_uid = {} # map from message id to uid use in history. Key: (full_jid,message_id) Value: uid
self.conn_deferred = defer.Deferred()
self._progress_cb = {} # callback called when a progress is requested (key = progress id)