Cacher¶
The Cacher classes’ aim is to save some data to files for long-term usage. One could see them as very simplistic databases.
Model Usage¶
As for the logger, if a cacher was initialized by the settings while running colifrapy, “cache” will be a reserved attribute of any of your models.
It is possible to register more than one cache instance with the Settings class. To achieve this, see cache settings.
from colifrapy import Model
class MyModel(Model):
def test(self):
# We assume that a line cache file containing
# the string "Hello" was loaded
print self.cache.get()
>>> 'Hello'
Standalone Usage¶
You can also use the Cacher classes as standalones rather than within colifrapy’s architecture.
from colifrapy import LineCacher, YAMLCacher
line_cache = LineCacher(options)
yaml_cache = YAMLCacher(options)
Modes¶
Line Cacher¶
The Line Cacher consists in a strict one-liner file and is aimed at storing really simple data.
Example of cache file content
1245
YAML Cacher¶
The YAML Cacher is designed to store more complex states of data and organized in a key-value fashion. The readability of the YAML file format makes the cache file easy to manually modify if needed. This is also possible to create deep nested data structures that will be accessible by paths.
Example of cache file content
number_of_tries: 14
countries:
albania: True
united_kingdom: "Hello"
Options¶
Here are the possible options you may pass to the Cacher classes constructors :
- directory
(string)
directory where you want to store your cache
default: “cache/”
- filename
(string)
name of the cache file
default: “cache.txt” or “cache.yml”
- auto_write
(boolean)
whether you want your cache to be automatically written when changed or not. If set to False, you’ll have to write the invoke the cache writing manually.
default: False
Methods¶
Line Cacher¶
from colifrapy import Model
class MyModel(Model):
def test(self):
# Setting cache
self.cache.set('Hello')
# Getting cache
print self.cache.get()
>>> 'Hello'
# Writing to cache
# N.B. : Useless if auto_write is set to True
self.cache.write()
# Deleting cache
self.cache.delete()
# Reading and writing filters
# Example of a single date cached
date_format = "%Y/%m/%d %H:%M:%S"
self.cache.setReadingFilter(lambda x: datetime.strptime(x, date_format))
self.cache.setWritingFilter(lambda x: x.strftime(date_format))
YAML Cacher¶
from colifrapy import Model
class MyModel(Model):
def test(self):
# Setting cache
self.cache.set("one", "red")
self.cache.set("two:deep", "blue")
# Getting cache
print self.cache.get("one")
>>> "red"
print self.cache.get("two")
>>> {"deep" : "blue"}
print self.cache.get("two:deep")
>>> "blue"
print self.cache
>>> {"one" : "red", "two" : {"deep" : "blue"}}
# Unset path
self.cache.unset("two")
print self.cache
>>> {"one" : "red"}
# Overwriting cache
self.cache.overwrite({'other' : 'structure'})
print self.cache
>>> {'other' : 'structure'}
# Writing to cache
# N.B. : Useless if auto_write is set to True
self.cache.write()
# Deleting cache
self.cache.delete()