Skip to content

Commit 178da86

Browse files
author
Jerjou Cheng
committed
Add snippets for ndb cache doc
1 parent 494750d commit 178da86

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
16+
def set_in_process_cache_policy(ctx, func):
17+
ctx.set_cache_policy(func)
18+
19+
20+
def set_memcache_policy(ctx, func):
21+
ctx.set_memcache_policy(func)
22+
23+
24+
def bypass_in_process_cache_for_account_entities(ctx):
25+
ctx.set_cache_policy(lambda key: key.kind() != 'Account')
26+
27+
28+
def set_datastore_policy(ctx, func):
29+
ctx.set_datastore_policy(func)
30+
31+
32+
def set_memcache_timeout_policy(ctx, func):
33+
ctx.set_memcache_timeout_policy(func)
34+
35+
36+
def clear_cache(ctx):
37+
ctx.clear_cache()

appengine/ndb/cache/snippets_test.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
context = ndb.get_context()
21+
22+
def policy(key):
23+
return 1 == 1
24+
25+
snippets.set_in_process_cache_policy(context, policy)
26+
assert policy == context.get_cache_policy()
27+
28+
29+
def test_set_memcache_policy(testbed):
30+
context = ndb.get_context()
31+
32+
def policy(key):
33+
return 1 == 2
34+
35+
snippets.set_memcache_policy(context, policy)
36+
assert policy == context.get_memcache_policy()
37+
38+
39+
def test_bypass_in_process_cache_for_account_entities(testbed):
40+
context = ndb.get_context()
41+
assert context.get_cache_policy() == context.default_cache_policy
42+
snippets.bypass_in_process_cache_for_account_entities(context)
43+
assert context.get_cache_policy() != context.default_cache_policy
44+
45+
46+
def test_set_datastore_policy(testbed):
47+
context = ndb.get_context()
48+
49+
def policy(key):
50+
return key is None
51+
52+
snippets.set_datastore_policy(context, policy)
53+
assert context.get_datastore_policy() == policy
54+
55+
56+
def test_set_memcache_timeout_policy(testbed):
57+
context = ndb.get_context()
58+
59+
def policy(key):
60+
return 1
61+
62+
snippets.set_memcache_timeout_policy(context, policy)
63+
assert context.get_memcache_timeout_policy() == policy
64+
65+
66+
def test_clear_cache(testbed):
67+
context = ndb.get_context()
68+
snippets.clear_cache(context)

0 commit comments

Comments
 (0)