provide simple caching
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3449 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
		
							parent
							
								
									b3036a885f
								
							
						
					
					
						commit
						4b2e3e175b
					
				
					 3 changed files with 110 additions and 0 deletions
				
			
		
							
								
								
									
										68
									
								
								util/cache.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								util/cache.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,68 @@
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#  Copyright (c) 2009 Helmut Merz helmutm@cy55.de
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#  This program is free software; you can redistribute it and/or modify
 | 
				
			||||||
 | 
					#  it under the terms of the GNU General Public License as published by
 | 
				
			||||||
 | 
					#  the Free Software Foundation; either version 2 of the License, or
 | 
				
			||||||
 | 
					#  (at your option) any later version.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#  This program is distributed in the hope that it will be useful,
 | 
				
			||||||
 | 
					#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
				
			||||||
 | 
					#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
				
			||||||
 | 
					#  GNU General Public License for more details.
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					#  You should have received a copy of the GNU General Public License
 | 
				
			||||||
 | 
					#  along with this program; if not, write to the Free Software
 | 
				
			||||||
 | 
					#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 | 
				
			||||||
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					A simple caching mechanism.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$Id$
 | 
				
			||||||
 | 
					"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from cybertools.util.date import getTimeStamp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					INVALID = object()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CacheManager(dict):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					manager = CacheManager()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CacheItem(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __init__(self, identifier, value=INVALID, lifetime=3600):
 | 
				
			||||||
 | 
					        self.identifier = identifier
 | 
				
			||||||
 | 
					        self.lifetime = lifetime
 | 
				
			||||||
 | 
					        self.set(value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def set(self, value):
 | 
				
			||||||
 | 
					        self.value = value
 | 
				
			||||||
 | 
					        self.modified = getTimeStamp()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get(self):
 | 
				
			||||||
 | 
					        self.check()
 | 
				
			||||||
 | 
					        return self.value
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def check(self):
 | 
				
			||||||
 | 
					        if getTimeStamp() - self.modified > self.lifetime:
 | 
				
			||||||
 | 
					            self.value = INVALID
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def cache(getIdentifier, lifetime=3600):
 | 
				
			||||||
 | 
					    def _cache(fct):
 | 
				
			||||||
 | 
					        def __cache(*args, **kw):
 | 
				
			||||||
 | 
					            id = getIdentifier(*args)
 | 
				
			||||||
 | 
					            item = manager.setdefault(id, CacheItem(id, lifetime=lifetime))
 | 
				
			||||||
 | 
					            value = item.get()
 | 
				
			||||||
 | 
					            if value is INVALID:
 | 
				
			||||||
 | 
					                value = fct(*args, **kw)
 | 
				
			||||||
 | 
					                item.set(value)
 | 
				
			||||||
 | 
					            return value
 | 
				
			||||||
 | 
					        return __cache
 | 
				
			||||||
 | 
					    return _cache
 | 
				
			||||||
							
								
								
									
										41
									
								
								util/cache.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								util/cache.txt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,41 @@
 | 
				
			||||||
 | 
					============
 | 
				
			||||||
 | 
					Data Caching
 | 
				
			||||||
 | 
					============
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$Id$
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  >>> from cybertools.util.cache import cache
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  >>> @cache(lambda *args: 'calc')
 | 
				
			||||||
 | 
					  ... def calculate():
 | 
				
			||||||
 | 
					  ...     print 'calculating'
 | 
				
			||||||
 | 
					  ...     return 42
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  >>> calculate()
 | 
				
			||||||
 | 
					  calculating
 | 
				
			||||||
 | 
					  42
 | 
				
			||||||
 | 
					  >>> calculate()
 | 
				
			||||||
 | 
					  42
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  >>> class Demo(object):
 | 
				
			||||||
 | 
					  ...     id = '4711'
 | 
				
			||||||
 | 
					  ...     def getId(self, *args):
 | 
				
			||||||
 | 
					  ...         return self.id
 | 
				
			||||||
 | 
					  ...     @cache(getId)
 | 
				
			||||||
 | 
					  ...     def calculate(self):
 | 
				
			||||||
 | 
					  ...         print 'calculating'
 | 
				
			||||||
 | 
					  ...         return 42
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  >>> demo = Demo()
 | 
				
			||||||
 | 
					  >>> demo.calculate()
 | 
				
			||||||
 | 
					  calculating
 | 
				
			||||||
 | 
					  42
 | 
				
			||||||
 | 
					  >>> demo.calculate()
 | 
				
			||||||
 | 
					  42
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  >>> demo.id = '4712'
 | 
				
			||||||
 | 
					  >>> demo.calculate()
 | 
				
			||||||
 | 
					  calculating
 | 
				
			||||||
 | 
					  42
 | 
				
			||||||
 | 
					  >>> demo.calculate()
 | 
				
			||||||
 | 
					  42
 | 
				
			||||||
| 
						 | 
					@ -20,6 +20,7 @@ def test_suite():
 | 
				
			||||||
        #doctest.DocTestSuite(cybertools.util.property, optionflags=flags),
 | 
					        #doctest.DocTestSuite(cybertools.util.property, optionflags=flags),
 | 
				
			||||||
        doctest.DocFileSuite('adapter.txt', optionflags=flags),
 | 
					        doctest.DocFileSuite('adapter.txt', optionflags=flags),
 | 
				
			||||||
        doctest.DocFileSuite('aop.txt', optionflags=flags),
 | 
					        doctest.DocFileSuite('aop.txt', optionflags=flags),
 | 
				
			||||||
 | 
					        doctest.DocFileSuite('cache.txt', optionflags=flags),
 | 
				
			||||||
        doctest.DocFileSuite('config.txt', optionflags=flags),
 | 
					        doctest.DocFileSuite('config.txt', optionflags=flags),
 | 
				
			||||||
        doctest.DocFileSuite('defer.txt', optionflags=flags),
 | 
					        doctest.DocFileSuite('defer.txt', optionflags=flags),
 | 
				
			||||||
        doctest.DocFileSuite('format.txt', optionflags=flags),
 | 
					        doctest.DocFileSuite('format.txt', optionflags=flags),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue