redis simple to understand and simple to use

redis database

Why learn redis
"""
1, redis no-sql database memory is high compared to a hard disk database efficiency mysql
2, the configuration database using the memory value without directly using the memory, data storage is redis manageable
3, memcache is also the in-memory database, and is used by default django database memcache, memcache replaced by redis route is very simple, which is more powerful
    redis supports more data types
    redis own caching mechanism, a database system crash data also can retrieve functions appear
    redis persistent data can be done automatically (self data persistence features)
    the data expiration time redis mechanism itself can be completed
"""
redis installation
Note: The official website to install the system version, you can download the corresponding installation package
Basic commands
Start Services:
 >: Redis-Server &

The client starts the connection Redis 
>: Redis-CLI
>: Redis-CLI database -H -n localhost -p 6379 numbers (0 to 15 ) After the successful handover connection database >: Select number database
redis data types
"""
Supported data types: String, Hash, List, Set, Sorted Set

String: store all other data types can not be stored
Hash: storing data in the form of key-value, similar to the dictionary
List: storing data in the form of a series of orderly value, a list (array)
Set: a series of disorderly value is stored in the form of data collection
Sorted Set: storing a reference value are arranged in the form of data, ranking
"""
String Manipulation
"" " Important method
set:key value
get:key
mset:k1 v1 k2 v2 ... kn vn
mget:k1 k2 ... kn
setex:key exp value
"""
Hashing
"""
Common methods:
Tenzin: hset key field value
Single check: hget key field
All keys: hgetall key
Single deletion: hdel key field
所有key:hkeys key
All values: hvals key
"""
List of operations
"""
Right by: rpush key v1 v2 ... vn
Left by: lpush key v1 v2 ... vn
Modify: lset key index value
Left deleted: lpop key
Right deleted: rpop key
插入:linsert key before|after old_value new_value
Range: lrange key begin_index end_index
"""
Set operations
"""
By: sadd key v1 v2 ... vn
Set difference: sdiff key1 key2
Union: sinter key1 key2
Intersection: sunion key1 key2
查:smembers key
Random deleted: spop key
"""
Ordered set
"""
增: zadd key SCORE1 SCORE2 value1 value2 ... to score surpluses
The number range: zcount key begin_score end_score
Top low to high: zrange key begin_index end_index
Ranking high to low: zrevrange key begin_index end_index
"""

python using redis

rely
>: pip3 install redis
Use directly
import redis
r = redis.Redis(host='127.0.0.1', port=6379)
Connection pool usage
import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
Cache Use: To install additional django-redis
# 1.将缓存存储位置配置到redis中:settings.py
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "CONNECTION_POOL_KWARGS": {"max_connections": 100}
        }
    }
}

# 2. Operation cache module cache direct operation: the views.py 
from django.core.cache Import cache   # binding profile pluggable achieve 
# stored token, the expiration time may be provided directly 
cache.set ( ' token ' , ' header.payload .signature ' , 10 )
 # remove token 
token = cache.get ( ' token ' )

note:

Be sure to configure CHAHES in settings.py, the django cache into redis

There will be a problem with incorrect data type native redis, deposit does not go

Redis through cache to be stored in terms of data

 

Guess you like

Origin www.cnblogs.com/wangnanfei/p/11768497.html