Python's flask framework uses Redis to configure methods for data caching _redis

Source: Internet
Author: User
Tags redis

Redis is a high-performance key-value storage system released under the BSD Open source protocol. Data is read into memory to improve access efficiency. Redis performance is extremely high-energy support for more than 100k+ per second read and write frequency, but also support the notification key expiration and so on features, so it is suitable for caching.

Download installation

Download compressed package using wget according to Redis Chinese network

$ wget http://download.redis.io/releases/redis-3.0.5.tar.gz
$ tar xzf redis-3.0.5.tar.gz
$ cd redis-3.0.5
$ make

Binary files are compiled in the SRC directory. You can start by using the command:

$ src/redis-server

This allows you to see that the Redis service is up and that the default port is 6379, which can be reids through the client operation.

$ src/redis-cli
redis> set foo bar
ok
redis> get foo
"Bar"

Flask Configuration Redis

First you have to download the Flask cache plugin Flask-cache and use the PIP download.

sudo pip install Flask_cache

Extended Flask_cache for application

From flask import flask from
flask.ext.cache Import cache

cache = cache ()

config = {
  ' cache_type ': ' Redis ',
  ' cache_redis_host ': ' 127.0.0.1 ',
  ' cache_redis_port ': 6379,
  ' cache_redis_db ': ',
  ' Cache_redis_ PASSWORD ': '

app = Flask (__name__)
app.config.from_object (config)
Cache.init_app (APP)

@app. Route ('/')
@cache. Cached (timeout=60*2)
def index ():
  name = ' Mink ' return
  name

if __ name__ = = ' __main__ ':
  app.run ()

Using the adorner cached () to decorate the view function, parameter timeout to set the expiration time, this article uses two minutes for cache time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.