diff --git a/appengine/memcache/snippets/snippets.py b/appengine/memcache/snippets/snippets.py new file mode 100644 index 000000000000..a4ce4867f51a --- /dev/null +++ b/appengine/memcache/snippets/snippets.py @@ -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] diff --git a/appengine/memcache/snippets/snippets_test.py b/appengine/memcache/snippets/snippets_test.py new file mode 100644 index 000000000000..fae6ae872a6c --- /dev/null +++ b/appengine/memcache/snippets/snippets_test.py @@ -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