Skip to content

Commit

Permalink
Add memcache snippets (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
elibixby authored and Jon Wayne Parrott committed Apr 28, 2016
1 parent 7b9ee96 commit 2cdc3d6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
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

0 comments on commit 2cdc3d6

Please sign in to comment.