Re: CAS and Django cache

2011-10-27 Thread Tom Evans
On Wed, Oct 26, 2011 at 11:29 PM, dmitry b  wrote:
> Can I do check-and-set operations using Django's cache api?
>
>
> Thanks
> D.
>

Memcached reuse existing terminology and then complain that this
causes confusion. Their CAS stands for 'compare and set', rather than
the 'compare and swap' that you would assume.

Django's cache API is designed to work with a number of backends, not
all of which offer an atomic compare and set method, so there is no
Django API for accessing compare and set on the memcached backend.

You can access the memcached client via django though:

>>> from django.core import cache
>>> c=cache.get_cache('default')
>>> help(c._client.cas)
Help on method cas in module memcache:

cas(self, key, val, time=0, min_compress_len=0) method of
memcache.Client instance
Sets a key to a given value in the memcache if it hasn't been
altered since last fetched. (See L{gets}).
... (do this yourself to see the whole docs)


Malcolm: compare and set is not equivalent to add. Compare and set is
used to change a cached value atomically, as long as it has not
already changed from the value you think it has. Add only sets a value
if it has not already been set.

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: CAS and Django cache

2011-10-27 Thread Malcolm Box
Yes, get followed by set can lead to data loss. 

What you want is cache.add(). This sets the value if and only if there is no 
existing value. Its atomic on backends that support it - notably memcached.  

Sent from my iPhone, please excuse any typos

On 27 Oct 2011, at 07:26, Dan Julius  wrote:

> Couldn't that potentially overwrite a value set by a different thread?
> 
> Dan
> 
> On Thu, Oct 27, 2011 at 7:13 AM, Kurtis Mullins  
> wrote:
> umm, I'm not sure if "check-and-set" is some cache-specific lingo or not. But 
> if you want to see if a value isn't set, check to see if it's None type... 
> example:
> 
> if cache.get('key') is None:
> cache.set('key', 'value', cache_seconds)
> 
> Sorry if that's not at all what you're talking about :)
> 
> 
> On Wed, Oct 26, 2011 at 6:29 PM, dmitry b  wrote:
> Can I do check-and-set operations using Django's cache api?
> 
> 
> Thanks
> D.
> 
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: CAS and Django cache

2011-10-27 Thread Dan Julius
Couldn't that potentially overwrite a value set by a different thread?

Dan

On Thu, Oct 27, 2011 at 7:13 AM, Kurtis Mullins wrote:

> umm, I'm not sure if "check-and-set" is some cache-specific lingo or not.
> But if you want to see if a value isn't set, check to see if it's None
> type... example:
>
> if cache.get('key') is None:
> cache.set('key', 'value', cache_seconds)
>
> Sorry if that's not at all what you're talking about :)
>
>
> On Wed, Oct 26, 2011 at 6:29 PM, dmitry b  wrote:
>
>> Can I do check-and-set operations using Django's cache api?
>>
>>
>> Thanks
>> D.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-users@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: CAS and Django cache

2011-10-26 Thread Kurtis Mullins
umm, I'm not sure if "check-and-set" is some cache-specific lingo or not.
But if you want to see if a value isn't set, check to see if it's None
type... example:

if cache.get('key') is None:
cache.set('key', 'value', cache_seconds)

Sorry if that's not at all what you're talking about :)

On Wed, Oct 26, 2011 at 6:29 PM, dmitry b  wrote:

> Can I do check-and-set operations using Django's cache api?
>
>
> Thanks
> D.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



CAS and Django cache

2011-10-26 Thread dmitry b
Can I do check-and-set operations using Django's cache api?


Thanks
D.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.