Skip to content

Commit 2cdc3d6

Browse files
elibixbyJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Add memcache snippets (#295)
1 parent 7b9ee96 commit 2cdc3d6

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START import]
16+
from google.appengine.api import memcache
17+
# [END import]
18+
19+
20+
def query_for_data():
21+
return 'data'
22+
23+
24+
# [START get_data]
25+
def get_data():
26+
data = memcache.get('key')
27+
if data is not None:
28+
return data
29+
else:
30+
data = query_for_data()
31+
memcache.add('key', data, 60)
32+
return data
33+
# [END get_data]
34+
35+
36+
def add_values():
37+
# [START add_values]
38+
# Add a value if it doesn't exist in the cache
39+
# with a cache expiration of 1 hour.
40+
memcache.add(key="weather_USA_98105", value="raining", time=3600)
41+
42+
# Set several values, overwriting any existing values for these keys.
43+
memcache.set_multi(
44+
{"USA_98115": "cloudy", "USA_94105": "foggy", "USA_94043": "sunny"},
45+
key_prefix="weather_",
46+
time=3600
47+
)
48+
49+
# Atomically increment an integer value.
50+
memcache.set(key="counter", value=0)
51+
memcache.incr("counter")
52+
memcache.incr("counter")
53+
memcache.incr("counter")
54+
# [END add_values]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.api import memcache
16+
from mock import patch
17+
import snippets
18+
19+
SNIPPET_VALUES = {
20+
"weather_USA_98105": "raining",
21+
"weather_USA_98115": "cloudy",
22+
"weather_USA_94105": "foggy",
23+
"weather_USA_94043": "sunny",
24+
"counter": 3,
25+
}
26+
27+
28+
@patch('snippets.query_for_data', return_value='data')
29+
def test_get_data_not_present(query_fn, testbed):
30+
data = snippets.get_data()
31+
query_fn.assert_called_once_with()
32+
assert data == 'data'
33+
memcache.delete('key')
34+
35+
36+
@patch('snippets.query_for_data', return_value='data')
37+
def test_get_data_present(query_fn, testbed):
38+
memcache.add('key', 'data', 9000)
39+
data = snippets.get_data()
40+
query_fn.assert_not_called()
41+
assert data == 'data'
42+
memcache.delete('key')
43+
44+
45+
def test_add_values(testbed):
46+
snippets.add_values()
47+
for key, value in SNIPPET_VALUES.iteritems():
48+
assert memcache.get(key) == value

0 commit comments

Comments
 (0)