Skip to content

Commit b120ec6

Browse files
committed
Functionally complete sample
1 parent 4799a60 commit b120ec6

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

  • appengine/standard/migration/memorystore

appengine/standard/migration/memorystore/main.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ def query_for_data():
4343

4444
def get_data(cache_key):
4545
## data = memcache.get('key')
46-
print("Getting data for {}".format(cache_key))
4746
data = client.get(cache_key)
4847

4948
if data is not None:
50-
print("Cache hit!")
5149
return data.decode()
5250
else:
53-
print("Cache miss.")
5451
data = query_for_data()
5552
## memcache.add('key', data, 60)
5653
client.set(cache_key, data, ex=60)
@@ -75,22 +72,20 @@ def add_values(values, expires=3600):
7572
## time=3600
7673
## )
7774
# Redis mset is similar to memcache.set_multi, but cannot set expirations
78-
print("Setting values: {}".format(values))
7975
client.mset(values)
8076

8177
# Rather than set expiration with separate operations for each key, batch
8278
# them using pipeline
8379
with client.pipeline() as pipe:
8480
for name in values:
85-
print("Setting expiration for {}".format(name))
86-
pipe.pexpire(name, 3600)
81+
pipe.pexpire(name, expires * 1000) # Time in milliseconds
8782
pipe.execute()
8883

8984

90-
def increment_counter(name, expires, value=0):
85+
def increment_counter(name, expires=60, value=0):
9186
# Atomically increment an integer value.
9287
## memcache.set(key="counter", value=0)
93-
client.set(name, expires, value=value)
88+
client.set(name, value, ex=expires)
9489
## memcache.incr("counter")
9590
client.incr(name)
9691
## memcache.incr("counter")
@@ -124,6 +119,8 @@ def savedata():
124119
key = request.form['key']
125120
value = request.form['value']
126121
add_values({key: value})
122+
if key == 'counter':
123+
increment_counter('counter', expires=60, value=value)
127124
return redirect('/')
128125

129126

0 commit comments

Comments
 (0)