Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memcache snippets #295

Merged
merged 2 commits into from
Apr 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions appengine/memcache/snippets/snippets.py
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]
48 changes: 48 additions & 0 deletions appengine/memcache/snippets/snippets_test.py
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