Changes between Version 4 and Version 5 of TracDev/CacheManager
- Timestamp:
- Feb 23, 2016, 8:20:26 PM (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
TracDev/CacheManager
v4 v5 1 = Caching and cache invalidation =1 = Caching and cache invalidation 2 2 3 Trac uses various caches at the component level, in order to speed-up costly tasks. Some examples are the ticket fields cache (#6436), others are the InterMapTxt cache, the user permission cache,the oldest example being the Wiki page cache.3 Trac uses various caches at the component level, in order to speed-up costly tasks. Some examples are the ticket fields cache (#6436), the InterMapTxt cache, the user permission cache, and the oldest example being the Wiki page cache. 4 4 5 Those cache are held at the level of `Component` instances. For a given class, there's one such instance per environment in any given server process. The first thing to take into account here is that those caches must be safely accessed and modified when accessed by concurrent threads (in multi-threaded web front ends, that is).5 Those caches are held at the level of `Component` instances. For a given class, there's one such instance per environment in any given server process. The first thing to take into account here is that those caches must be safely accessed and modified when accessed by concurrent threads (in multi-threaded web front ends, that is). 6 6 7 But due to the always possible concurrent access at the underlying database level by '''multiple processes''', there's also a need to maintain a consistency and up-to-date status of those caches across all processes involved. Otherwise, you might do a change by the way of one request and the next request (even the GET following a redirect after your POST!) might be handled by a different server process which has a different "view" of the application state.7 But because of the possibility of concurrent access at the underlying database level by '''multiple processes''', there is also a need to maintain a consistency and up-to-date status of those caches across all processes involved. Otherwise, you might do a change by the way of one request and the next request (even the GET following a redirect after your POST!) might be handled by a different server process which has a different "view" of the application state. 8 8 9 9 This doesn't even have to imply a multi-process server setup, as all what is needed is e.g. a modification of the database done using trac-admin. 10 10 11 == The Cache Manager == 12 Starting with Trac [milestone:0.12] (more precisely r8071), we introduced a !CacheManager component. 13 That component is mostly transparent to the end developer, which only has to deal with a decorator 14 that can be used to create ''cached attributes''. 11 == The Cache Manager 12 13 Starting with Trac [milestone:0.12] (more precisely r8071), we introduced a !CacheManager component. That component is mostly transparent to the end developer, which only has to deal with a decorator that can be used to create ''cached attributes''. 15 14 16 15 * '''Creating a cached attribute''' is done by defining a retrieval function and decorating it with the '''`@cached`''' decorator. For example, for the wiki page names: 17 {{{ 18 #!python 16 {{{#!python 19 17 @cached 20 18 def pages(self, db): … … 26 24 27 25 * '''Invalidating a cached attribute''' is done by '''`del`'''eting the attribute: 28 {{{ 29 #!python 26 {{{#!python 30 27 def wiki_page_added(self, page): 31 28 if not self.has_page(page.name): … … 43 40 * The `cache` table is read the first time a cached attribute is accessed during a request. This avoids slowing down requests that don't touch cached attributes, like requests for static content for example. 44 41 45 46 42 ---- 47 43 See also TracDev/Proposals/CacheInvalidation for the history of the implementation details.