Hi Carl,

> Den 10/02/2015 kl. 18.34 skrev Carl Meyer <c...@oddbird.net 
> <mailto:c...@oddbird.net>>:
> 
> I assume you're using the 'db' cache backend? Otherwise, it wouldn't
> make sense to expect transactions to affect cache calls at all.

Yes, I'm using the db backend.

> The difference between the snippets is that you are doing a conditional
> branch based on the return value of the cache add, but your set_val
> helper method always returns None, it doesn't return the return value of
> cache.add().

Wow, *that* was embarrassing :-) You see what you want to see, I guess...

I'm still having issues with this, though. I reduced my own code to the 
following. The intent of the code is to use the cache to implement 
multiprocess/multiserver locking:

from django.db import transaction
from django.core.cache import cache

class CacheError(Exception):
    pass

cache.delete('foo')
while True:
    print('Trying to cache foo')
    try:
        with transaction.atomic():
            if cache.get('foo'):
                raise CacheError()
            print('adding foo to cache')
            assert cache.add(key='foo', value='bar')
            print('foo cached')
        time.sleep(random.random())
        with transaction.atomic():
            if cache.get('foo'):
                cache.delete('foo')
    except CacheError:
        print('Failed to cache foo')
    time.sleep(random.random())

Running this in parallel in two processes on the same machine returns this 
after a while:

Process A:
Trying to cache foo
2015-02-10 21:02:25,781 DEBUG    (0.001) SELECT cache_key, value, expires FROM 
"dispatch_cache" WHERE cache_key = 'foo'; args=['foo']
adding foo to cache
2015-02-10 21:02:25,782 DEBUG    (0.000) SELECT COUNT(*) FROM "dispatch_cache"; 
args=None
2015-02-10 21:02:25,782 DEBUG    (0.000) SAVEPOINT "s140735261451008_x1"; 
args=None
2015-02-10 21:02:25,783 DEBUG    (0.000) SELECT cache_key, expires FROM 
"dispatch_cache" WHERE cache_key = 'foo'; args=['foo']
2015-02-10 21:02:25,784 DEBUG    (0.000) INSERT INTO "dispatch_cache" 
(cache_key, value, expires) VALUES ('foo', 'gASVBwAAAAAAAACMA2JhcpQu', 
'9999-12-31 23:59:59'); args=['foo', 'gASVBwAAAAAAAACMA2JhcpQu', '9999-12-31 
23:59:59']
2015-02-10 21:02:25,784 DEBUG    (0.000) RELEASE SAVEPOINT 
"s140735261451008_x1"; args=None
foo cached
2015-02-10 21:02:26,771 DEBUG    (0.000) SELECT cache_key, value, expires FROM 
"dispatch_cache" WHERE cache_key = 'foo'; args=['foo']
2015-02-10 21:02:26,772 DEBUG    (0.000) DELETE FROM "dispatch_cache" WHERE 
cache_key = 'foo'; args=['foo']


Process B:
Trying to cache foo
2015-02-10 21:02:25,782 DEBUG    (0.000) SELECT cache_key, value, expires FROM 
"dispatch_cache" WHERE cache_key = 'foo'; args=['foo']
adding foo to cache
2015-02-10 21:02:25,783 DEBUG    (0.000) SELECT COUNT(*) FROM "dispatch_cache"; 
args=None
2015-02-10 21:02:25,784 DEBUG    (0.000) SAVEPOINT "s140735261451008_x1"; 
args=None
2015-02-10 21:02:25,784 DEBUG    (0.000) SELECT cache_key, expires FROM 
"dispatch_cache" WHERE cache_key = 'foo'; args=['foo']
2015-02-10 21:02:25,791 DEBUG    (0.007) INSERT INTO "dispatch_cache" 
(cache_key, value, expires) VALUES ('foo', 'gASVBwAAAAAAAACMA2JhcpQu', 
'9999-12-31 23:59:59'); args=['foo', 'gASVBwAAAAAAAACMA2JhcpQu', '9999-12-31 
23:59:59']
2015-02-10 21:02:25,792 DEBUG    (0.000) ROLLBACK TO SAVEPOINT 
"s140735261451008_x1"; args=None
Traceback (most recent call last):
  File "tmp.py", line 30, in <module>
    assert cache.add(key='foo', value='bar')
AssertionError


I don't see how this is possible when I'm using transaction.atomic().


Thanks,
Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/FD568626-E3C3-4026-9617-3EDB24F037D8%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to