|
| 1 | +# Copyright 2020 Google LLC |
| 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 | +# This code contrasts how Python 2.7 apps using the memcache library can be |
| 16 | +# changed to use the Memorycache for Redis API and libraries, instead, under |
| 17 | +# either Python 2.7 or Python 3.6 or later. |
| 18 | +# |
| 19 | +# The examples are taken from the appengine/standard/memcache/snippets example |
| 20 | +# in this repository. The lines of code from the old example are included here |
| 21 | +# as comments, preceded by ## to distinguish them. |
| 22 | +# |
| 23 | +# The samples are wrapped in a Flask web app to run in App Engine. |
| 24 | + |
| 25 | +## from google.appengine.api import memcache |
| 26 | +import os |
| 27 | +import redis |
| 28 | +import time |
| 29 | + |
| 30 | +from flask import Flask, redirect, render_template, request |
| 31 | + |
| 32 | +redis_host = os.environ.get('REDIS_HOST', 'localhost') |
| 33 | +redis_port = os.environ.get('REDIS_PORT', '6379') |
| 34 | +client = redis.Redis(host=redis_host, port=redis_port) |
| 35 | + |
| 36 | + |
| 37 | +# Dummy store that this sample shows being cached |
| 38 | +def query_for_data(): |
| 39 | + # Add artificial delay so user can see when cache has been used |
| 40 | + time.sleep(5) |
| 41 | + return 'prestored value' |
| 42 | + |
| 43 | + |
| 44 | +def get_data(cache_key): |
| 45 | + ## data = memcache.get('key') |
| 46 | + data = client.get(cache_key) |
| 47 | + |
| 48 | + if data is not None: |
| 49 | + return data.decode() |
| 50 | + else: |
| 51 | + data = query_for_data() |
| 52 | + ## memcache.add('key', data, 60) |
| 53 | + client.set(cache_key, data, ex=60) |
| 54 | + |
| 55 | + return data |
| 56 | + |
| 57 | + |
| 58 | +def add_values(values, expires=3600): |
| 59 | + # Add a value if it doesn't exist in the cache |
| 60 | + # with a cache expiration of 1 hour. |
| 61 | + ## memcache.add(key="weather_USA_98105", value="raining", time=3600) |
| 62 | + ## # Remove one of the values and set by itself first |
| 63 | + ## first_key = values.keys()[0] |
| 64 | + ## first_value = values[first_key] |
| 65 | + ## del values[first_key] |
| 66 | + ## memcache.add(first_key, first_value, ex=expires) |
| 67 | + ## |
| 68 | + ## # Set several values, overwriting any existing values for these keys. |
| 69 | + ## memcache.set_multi( |
| 70 | + ## {"USA_98115": "cloudy", "USA_94105": "foggy", "USA_94043": "sunny"}, |
| 71 | + ## key_prefix="weather_", |
| 72 | + ## time=3600 |
| 73 | + ## ) |
| 74 | + # Redis mset is similar to memcache.set_multi, but cannot set expirations |
| 75 | + client.mset(values) |
| 76 | + |
| 77 | + # Rather than set expiration with separate operations for each key, batch |
| 78 | + # them using pipeline |
| 79 | + with client.pipeline() as pipe: |
| 80 | + for name in values: |
| 81 | + pipe.pexpire(name, expires * 1000) # Time in milliseconds |
| 82 | + pipe.execute() |
| 83 | + |
| 84 | + |
| 85 | +def increment_counter(name, expires=60, value=0): |
| 86 | + # Atomically increment an integer value. |
| 87 | + ## memcache.set(key="counter", value=0) |
| 88 | + client.set(name, value, ex=expires) |
| 89 | + ## memcache.incr("counter") |
| 90 | + client.incr(name) |
| 91 | + ## memcache.incr("counter") |
| 92 | + client.incr(name) |
| 93 | + ## memcache.incr("counter") |
| 94 | + client.incr(name) |
| 95 | + |
| 96 | + |
| 97 | +# Web app to invoke above samples follows |
| 98 | +app = Flask(__name__) |
| 99 | + |
| 100 | + |
| 101 | +@app.route('/', methods=['GET']) |
| 102 | +def home(): |
| 103 | + return render_template('index.html') |
| 104 | + |
| 105 | + |
| 106 | +@app.route('/showdata', methods=['GET']) |
| 107 | +def showdata(): |
| 108 | + data = get_data('data') |
| 109 | + |
| 110 | + values = {} |
| 111 | + for key in client.keys(): |
| 112 | + if key == 'data': |
| 113 | + next |
| 114 | + values[key.decode()] = client.get(key).decode() |
| 115 | + |
| 116 | + return render_template('showdata.html', data=data, values=values) |
| 117 | + |
| 118 | + |
| 119 | +@app.route('/showdata', methods=['POST']) |
| 120 | +def savedata(): |
| 121 | + key = request.form['key'] |
| 122 | + value = request.form['value'] |
| 123 | + add_values({key: value}) |
| 124 | + if key == 'counter': |
| 125 | + increment_counter('counter', expires=60, value=value) |
| 126 | + return redirect('/') |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == '__main__': |
| 130 | + # This is used when running locally. |
| 131 | + app.run(host='127.0.0.1', port=8080, debug=True) |
0 commit comments