From c90575538170019e102921a071b8a310e2579d37 Mon Sep 17 00:00:00 2001 From: elibixby Date: Wed, 27 Apr 2016 16:37:18 -0700 Subject: [PATCH 1/2] Add memcache snippets --- appengine/memcache/snippets.py | 40 +++++++++++++++++++++++++++++ appengine/memcache/snippets_test.py | 34 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 appengine/memcache/snippets.py create mode 100644 appengine/memcache/snippets_test.py diff --git a/appengine/memcache/snippets.py b/appengine/memcache/snippets.py new file mode 100644 index 000000000000..b19529ee74df --- /dev/null +++ b/appengine/memcache/snippets.py @@ -0,0 +1,40 @@ +# [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_test.py b/appengine/memcache/snippets_test.py new file mode 100644 index 000000000000..9d34d28da075 --- /dev/null +++ b/appengine/memcache/snippets_test.py @@ -0,0 +1,34 @@ +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 From bf3e9bbd70be0cbc33e099bdbe23b34dd7041a68 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Thu, 28 Apr 2016 10:28:23 -0700 Subject: [PATCH 2/2] Moving into subfolder, adding license headers. Change-Id: I999e32e3655d49c2d9b96d49adf6d9e4bf2123eb --- appengine/memcache/{ => snippets}/snippets.py | 14 ++++++++++++++ appengine/memcache/{ => snippets}/snippets_test.py | 14 ++++++++++++++ 2 files changed, 28 insertions(+) rename appengine/memcache/{ => snippets}/snippets.py (62%) rename appengine/memcache/{ => snippets}/snippets_test.py (60%) diff --git a/appengine/memcache/snippets.py b/appengine/memcache/snippets/snippets.py similarity index 62% rename from appengine/memcache/snippets.py rename to appengine/memcache/snippets/snippets.py index b19529ee74df..a4ce4867f51a 100644 --- a/appengine/memcache/snippets.py +++ b/appengine/memcache/snippets/snippets.py @@ -1,3 +1,17 @@ +# 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] diff --git a/appengine/memcache/snippets_test.py b/appengine/memcache/snippets/snippets_test.py similarity index 60% rename from appengine/memcache/snippets_test.py rename to appengine/memcache/snippets/snippets_test.py index 9d34d28da075..fae6ae872a6c 100644 --- a/appengine/memcache/snippets_test.py +++ b/appengine/memcache/snippets/snippets_test.py @@ -1,3 +1,17 @@ +# 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