Question about : Building_Scalable_Web_Applications_with_Google

2 views
Skip to first unread message

Sylvain

unread,
Sep 15, 2008, 11:43:19 AM9/15/08
to Google App Engine
Maybe a stupid question ;)

In this doc (how to create a counter), there is a line :
memcache.add(name, str(total), 60)

I wonder why there is a str(total) i.e. :
memcache.add(name, total, 60)

Thank you.

---- full code ---
def get_count(name):
total = memcache.get(name)
if total is None:
total = 0
for counter in Counter.gql('WHERE name = :1', name):
total += counter.count
memcache.add(name, str(total), 60)
return total

Sylvain

unread,
Sep 15, 2008, 11:47:57 AM9/15/08
to Google App Engine
I think i've found the solution in the doc, but then there is an issue
in the doc :
http://code.google.com/appengine/docs/memcache/overview.html

# Atomically increment an integer value.
memcache.set(key="counter", 0) and it should be :
memcache.set(key="counter", str(0)) ?

Regards


--------
incr(key, delta=1)

Atomically increments a key's value. Internally, the value is a
unsigned 64-bit integer. Memcache doesn't check 64-bit overflows. The
value, if too large, will wrap around.

The key must already exist in the cache to be incremented. To
initialize a counter, set() it to the initial value, as an ASCII
decimal integer. Future get()s of the key, post-increment, will still
be an ASCII decimal value.
-----------------

Marzia Niccolai

unread,
Sep 15, 2008, 3:17:42 PM9/15/08
to google-a...@googlegroups.com
Hi,

The example in the Building Scalable Web Applications slide is wrong.  If you are using the Memcache function 'incr' the value should be an integer.

-Marzia

Brett

unread,
Sep 16, 2008, 2:58:58 AM9/16/08
to Google App Engine

On Sep 15, 12:17 pm, "Marzia Niccolai" <ma...@google.com> wrote:
> The example in the Building Scalable Web Applications slide is wrong.  If
> you are using the Memcache function 'incr' the value should be an integer.

Both are correct. When you call memcache.incr, it will try to
interpret the string value as an integer. That code pre-dates the
smart handling of integer, long, and boolean types. App Engine now
does this automatically under the covers. The upstream memcache Python
interface does not, as I understand it.

Reply all
Reply to author
Forward
0 new messages