1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 Base classes for database handling
18 """
19
21 """
22 A fixed component of the DBConnector, handling one particular aspect of the
23 database. Instances of subclasses are assigned to attributes of the
24 DBConnector object, so that they are available at e.g., C{master.db.model}
25 or C{master.db.changes}. This parent class takes care of the necessary
26 backlinks and other housekeeping.
27 """
28
29 connector = None
30
32 self.db = connector
33 "backlink to the DBConnector object"
34
35
36 for method in dir(self.__class__):
37 o = getattr(self, method)
38 if isinstance(o, CachedMethod):
39 setattr(self, method, o.get_cached_method(self))
40
43 self.cache_name = cache_name
44 self.method = method
45
47 meth = self.method
48
49 meth_name = meth.__name__
50 cache = component.db.master.caches.get_cache(self.cache_name,
51 lambda key : meth(component, key))
52 def wrap(key, no_cache=0):
53 if no_cache:
54 return meth(component, key)
55 return cache.get(key)
56 wrap.__name__ = meth_name + " (wrapped)"
57 wrap.__module__ = meth.__module__
58 wrap.__doc__ = meth.__doc__
59 wrap.cache = cache
60 return wrap
61
63 """
64 A decorator for "getter" functions that fetch an object from the database
65 based on a single key. The wrapped method will only be called if the named
66 cache does not contain the key.
67
68 The wrapped function must take one argument (the key); the wrapper will
69 take a key plus an optional C{no_cache} argument which, if true, will cause
70 it to invoke the underlying method even if the key is in the cache.
71
72 The resulting method will have a C{cache} attribute which can be used to
73 access the underlying cache.
74
75 @param cache_name: name of the cache to use
76 """
77 return lambda method : CachedMethod(cache_name, method)
78