Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout per manager instance #7

Open
jsikorsky opened this issue Oct 20, 2015 · 0 comments
Open

Timeout per manager instance #7

jsikorsky opened this issue Oct 20, 2015 · 0 comments

Comments

@jsikorsky
Copy link

Hi,

I created a custom manager which caches get() and get_or_create() functions. Everything works just fine, but I'd like to use different timeouts for different models which use this manager. Consider the following example:

from django.db import models
from memoize import memoize

class CachingManager(models.Manager):
    def __init__(self, cache_timeout = 0):
        self.cache_timeout = cache_timeout
        memoizer = memoize(timeout=self.cache_timeout)
        self.get = memoizer(super(CachingManager, self).get)
        self.get_or_create = memoizer(super(CachingManager, self).get)
        super(CachingManager, self).__init__()


class Host(models.Model):
    hostname = models.CharField(max_length = 30,  unique = True)
    objects = models.Manager()    
    caching = CachingManager(cache_timeout = 5)

This raises IndexError:

....
  File "/usr/lib/python2.7/site-packages/django_memoize-1.2.0-py2.7.egg/memoize/__init__.py", line 239, in _memoize_kwargs_to_args
    arg = repr(args[0])
IndexError: tuple index out of range

I created a patch - seems to work for me, but I'm not sure if it won't break anything else:

diff -rup django_memoize-1.2.0-py2.7.egg.orig/memoize/__init__.py django_memoize-1.2.0-py2.7.egg/memoize/__init__.py
--- django_memoize-1.2.0-py2.7.egg.orig/memoize/__init__.py     2015-10-16 22:13:33.460610061 +0200
+++ django_memoize-1.2.0-py2.7.egg/memoize/__init__.py  2015-10-17 11:52:56.327081228 +0200
@@ -236,7 +236,7 @@ class Memoizer(object):
                 #: this supports instance methods for
                 #: the memoized functions, giving more
                 #: flexibility to developers
-                arg = repr(args[0])
+                arg = repr(argspec.args[i])
                 arg_num += 1
             elif argspec.args[i] in kwargs:
                 arg = kwargs.pop(argspec.args[i])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant