-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2016 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# [START import] | ||
from google.appengine.api import memcache | ||
# [END import] | ||
|
||
|
||
def query_for_data(): | ||
return 'data' | ||
|
||
|
||
# [START get_data] | ||
def get_data(): | ||
data = memcache.get('key') | ||
if data is not None: | ||
return data | ||
else: | ||
data = query_for_data() | ||
memcache.add('key', data, 60) | ||
return data | ||
# [END get_data] | ||
|
||
|
||
def add_values(): | ||
# [START add_values] | ||
# Add a value if it doesn't exist in the cache | ||
# with a cache expiration of 1 hour. | ||
memcache.add(key="weather_USA_98105", value="raining", time=3600) | ||
|
||
# Set several values, overwriting any existing values for these keys. | ||
memcache.set_multi( | ||
{"USA_98115": "cloudy", "USA_94105": "foggy", "USA_94043": "sunny"}, | ||
key_prefix="weather_", | ||
time=3600 | ||
) | ||
|
||
# Atomically increment an integer value. | ||
memcache.set(key="counter", value=0) | ||
memcache.incr("counter") | ||
memcache.incr("counter") | ||
memcache.incr("counter") | ||
# [END add_values] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2016 Google Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.appengine.api import memcache | ||
from mock import patch | ||
import snippets | ||
|
||
SNIPPET_VALUES = { | ||
"weather_USA_98105": "raining", | ||
"weather_USA_98115": "cloudy", | ||
"weather_USA_94105": "foggy", | ||
"weather_USA_94043": "sunny", | ||
"counter": 3, | ||
} | ||
|
||
|
||
@patch('snippets.query_for_data', return_value='data') | ||
def test_get_data_not_present(query_fn, testbed): | ||
data = snippets.get_data() | ||
query_fn.assert_called_once_with() | ||
assert data == 'data' | ||
memcache.delete('key') | ||
|
||
|
||
@patch('snippets.query_for_data', return_value='data') | ||
def test_get_data_present(query_fn, testbed): | ||
memcache.add('key', 'data', 9000) | ||
data = snippets.get_data() | ||
query_fn.assert_not_called() | ||
assert data == 'data' | ||
memcache.delete('key') | ||
|
||
|
||
def test_add_values(testbed): | ||
snippets.add_values() | ||
for key, value in SNIPPET_VALUES.iteritems(): | ||
assert memcache.get(key) == value |