Makes memoise work properly
This commit is contained in:
		
							parent
							
								
									27ab44ec44
								
							
						
					
					
						commit
						a267b60eb9
					
				
					 1 changed files with 21 additions and 9 deletions
				
			
		|  | @ -1,6 +1,8 @@ | ||||||
| import contextlib | import contextlib | ||||||
| import functools | import functools | ||||||
| 
 | 
 | ||||||
|  | from django.contrib.auth.models import User | ||||||
|  | 
 | ||||||
| 
 | 
 | ||||||
| class BatchController(object): | class BatchController(object): | ||||||
|     ''' Batches are sets of operations where certain queries for users may be |     ''' Batches are sets of operations where certain queries for users may be | ||||||
|  | @ -54,26 +56,36 @@ class BatchController(object): | ||||||
|     @classmethod |     @classmethod | ||||||
|     def memoise(cls, func): |     def memoise(cls, func): | ||||||
|         ''' Decorator that stores the result of the stored function in the |         ''' Decorator that stores the result of the stored function in the | ||||||
|         user's results cache until the batch completes. |         user's results cache until the batch completes. Keyword arguments are | ||||||
|  |         not yet supported. | ||||||
| 
 | 
 | ||||||
|         Arguments: |         Arguments: | ||||||
|             func (callable(user, *a, **k)): The function whose results we want |             func (callable(*a)): The function whose results we want | ||||||
|                 to store. ``user`` must be the first argument; this is used as |                 to store. The positional arguments, ``a``, are used as cache | ||||||
|                 the cache key. |                 keys. | ||||||
| 
 | 
 | ||||||
|         Returns: |         Returns: | ||||||
|             callable(user, *a, **k): The memosing version of ``func``. |             callable(*a): The memosing version of ``func``. | ||||||
| 
 | 
 | ||||||
|         ''' |         ''' | ||||||
| 
 | 
 | ||||||
|         @functools.wraps(func) |         @functools.wraps(func) | ||||||
|         def f(user, *a, **k): |         def f(*a): | ||||||
| 
 | 
 | ||||||
|  |             for arg in a: | ||||||
|  |                 if isinstance(arg, User): | ||||||
|  |                     user = arg | ||||||
|  |                     break | ||||||
|  |             else: | ||||||
|  |                 raise ValueError("One position argument must be a User") | ||||||
|  | 
 | ||||||
|  |             func_key = (func, tuple(a)) | ||||||
|             cache = cls.get_cache(user) |             cache = cls.get_cache(user) | ||||||
|             if func not in cache: |  | ||||||
|                 cache[func] = func(user, *a, **k) |  | ||||||
| 
 | 
 | ||||||
|             return cache[func] |             if func_key not in cache: | ||||||
|  |                 cache[func_key] = func(*a) | ||||||
|  | 
 | ||||||
|  |             return cache[func_key] | ||||||
| 
 | 
 | ||||||
|         return f |         return f | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Christopher Neugebauer
						Christopher Neugebauer