Skip to content

Commit 80e9e73

Browse files
committed
Merge pull request #282 from GoogleCloudPlatform/ndb
Add snippets for ndb cache doc
2 parents 53373b1 + 14dab35 commit 80e9e73

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

appengine/ndb/cache/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## App Engine Datastore NDB Cache Samples
2+
3+
This contains snippets used in the NDB cache documentation, demonstrating
4+
various operation on ndb caches.
5+
6+
<!-- auto-doc-link -->
7+
These samples are used on the following documentation page:
8+
9+
> https://cloud.google.com/appengine/docs/python/ndb/cache
10+
11+
<!-- end-auto-doc-link -->

appengine/ndb/cache/snippets.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import ndb
16+
17+
18+
def set_in_process_cache_policy(func):
19+
context = ndb.get_context()
20+
context.set_cache_policy(func)
21+
22+
23+
def set_memcache_policy(func):
24+
context = ndb.get_context()
25+
context.set_memcache_policy(func)
26+
27+
28+
def bypass_in_process_cache_for_account_entities():
29+
context = ndb.get_context()
30+
context.set_cache_policy(lambda key: key.kind() != 'Account')
31+
32+
33+
def set_datastore_policy(func):
34+
context = ndb.get_context()
35+
context.set_datastore_policy(func)
36+
37+
38+
def set_memcache_timeout_policy(func):
39+
context = ndb.get_context()
40+
context.set_memcache_timeout_policy(func)
41+
42+
43+
def clear_cache():
44+
context = ndb.get_context()
45+
context.clear_cache()

appengine/ndb/cache/snippets_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2016 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.appengine.ext import ndb
16+
import snippets
17+
18+
19+
def test_set_in_process_cache_policy(testbed):
20+
def policy(key):
21+
return 1 == 1
22+
23+
snippets.set_in_process_cache_policy(policy)
24+
assert policy == ndb.get_context().get_cache_policy()
25+
26+
27+
def test_set_memcache_policy(testbed):
28+
def policy(key):
29+
return 1 == 2
30+
31+
snippets.set_memcache_policy(policy)
32+
assert policy == ndb.get_context().get_memcache_policy()
33+
34+
35+
def test_bypass_in_process_cache_for_account_entities(testbed):
36+
context = ndb.get_context()
37+
assert context.get_cache_policy() == context.default_cache_policy
38+
snippets.bypass_in_process_cache_for_account_entities()
39+
assert context.get_cache_policy() != context.default_cache_policy
40+
41+
42+
def test_set_datastore_policy(testbed):
43+
def policy(key):
44+
return key is None
45+
46+
snippets.set_datastore_policy(policy)
47+
assert ndb.get_context().get_datastore_policy() == policy
48+
49+
50+
def test_set_memcache_timeout_policy(testbed):
51+
def policy(key):
52+
return 1
53+
54+
snippets.set_memcache_timeout_policy(policy)
55+
assert ndb.get_context().get_memcache_timeout_policy() == policy
56+
57+
58+
def test_clear_cache(testbed):
59+
snippets.clear_cache()

0 commit comments

Comments
 (0)