summaryrefslogtreecommitdiff
path: root/Cheetah/CacheRegion.py
blob: 5312fd711d0963ffa98e492b076805319ea01809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# $Id: CacheRegion.py,v 1.1 2006/12/18 23:06:54 dwelch Exp $
"""Cache holder classes for Cheetah:

Cache regions are defined using the #cache Cheetah directive. Each
cache region can be viewed as a dictionnary (keyed by cacheID)
handling at least one cache (the default one). It's possible to add
caches in a region by using the `varyBy` #cache directive parameter as
in the following example::

   #cache varyBy=$getArticleID()
     #def getArticle
         this is the article content.
     #end def
   #end cache

The code above will generate a CacheRegion, and depending on the
article id value, add some new sub-caches ad-hoc.

Meta-Data
================================================================================
Author: Philippe Normand <phil@base-art.net>
Version: $Revision: 1.1 $
Start Date: 2005/06/20
Last Revision Date: $Date: 2006/12/18 23:06:54 $
"""
__author__ = "Philippe Normand <phil@base-art.net>"
__revision__ = "$Revision: 1.1 $"[11:-2]

import md5

class CacheRegion:
    """ A `CacheRegion` stores some `Cache` instances.
    """
    
    def __init__(self):
        self.clear()
        
    def clear(self):
        " drop all the caches stored in this cache region "
        self.caches = {}
        
    def getCache(self, cacheID):
        """ Lazy access to a cache

            Try to find a cache in the stored caches. If it doesn't
            exist, it's created.
            Returns a `Cache` instance.
        """
        cacheID = md5.new(str(cacheID)).hexdigest()
        if not self.caches.has_key(cacheID):
            cache = Cache(cacheID)
            self.caches[cacheID] = cache
        return self.caches.get(cacheID)
    
class Cache:
    """ Cache class.

        A Cache is a container storing:

        - cacheID (string)
        - refreshTime (timestamp or None) : last time the cache was refreshed
        - data (string) : the content of the cache

    """
    
    def __init__(self, cacheID):
        self.setID(cacheID)
        self.clear()
        
    def clear(self):
        self.setData("")
        self.setRefreshTime(None)

    def getID(self):
        return self.cacheID

    def setID(self, cacheID):
        self.cacheID = cacheID
        
    def setData(self, data):
        self.data = data

    def getData(self):
        return self.data

    def setRefreshTime(self, time):
        self.refreshTime = time

    def getRefreshTime(self):
        return self.refreshTime