NoSQL (not only sql)
D: a class of emerging database does not support sql statement to store data in the form of key-value pairs
common product categories: Mongodb, Redis, Hbase hadoop
Redis
D: is an open source use ANISIC language, support network, it can also be based on memory persistence log database. Redis also supports the backup data, i.e., data backup master-slave mode
Advantages: reading, high performance; rich data types, support binary String, Lists, Hashes other
operation atomicity, supports key expires, notification characteristics .
Scenario: used for caching; to achieve a particular function, session sharing, shopping carts, product browsing history
U: Use common commands redis
download | wget http://download.redis.io/releases/redis-3.2.8.tar.gz |
Decompression | takes -zxvf Redis-3.2.8.tar.gz |
Compile and install (into the root directory of the extract from the redis | sudo make install |
After installation is complete, enter the directory / usr / local / bin View | cd /usr/local/bin ls -all |
Verify that the installation was successful (no error) | sudo redis-server |
View started redis-server | ps -ef | grep Redis server |
Specify the configuration file to start the server redis | sudo redis-server /etc/redis/redis.conf |
stop | sudo pkill -9 redis-server |
Redis client connects to the server | redis-cli |
A host port connected to the stationary | redis-cli -h host -p port --raw (raw view Chinese) |
Switching Database | select 2 |
Delete the current database of all key | flushdb |
Delete key database for all | flashall |
The data structure and operation behavior:
Redis is a key-value data structure is a key for each data
type is a string key, the key can not duplicate
the type divided into five values: ① string string② listing list③ set set④ zset⑤ ordered set of hash hash
operational behavior modification ① ② ③ get saved ④ delete
database operation
has a key command
Find key | keys * --- Find all key Keys * a * --- look with a key |
Determine whether there is key | exists key (return 1 exists, there is no return 0) |
Check the type of value | type key (five types) |
Delete key-value pairs | the key1 key2 |
Set an expiration time | setex key valid time (seconds) value |
Setting effective time | expire key valid time (sec) |
View bond remaining effective time | ttl key (time to live) |
String (string) Command
Set the key (if there is no add, if there is to modify) | set key value |
A plurality of keys | mset key1 value1 key2 value2 ... |
Added value | append aa haha |
Query Value | get key |
Obtaining a plurality of values according to the plurality of keys | mget key1 key2 ... |
List (list) command
Inserting data from the left | lpush key value1 value2 … 结果[value2, value1] |
Inserting data from the right | rpush key value1 value2 … 结果[value1, value2] |
Insert an element | linsert key before / after the value of existing elements to be inserted |
The value of the element count value of the secondary list that appears before removing the | Key COUNT value lrem (COUNT is greater than 0: to remove the tail from the head; count less than 0: to remove the tail from the head; count = 0: Remove all) |
Modifying the element value of the specified index | lset key index value |
Gets the list of elements within the specified range | lrange key index ending index start (inclusive) |
Interception elements in the list specified range | ltrim key index start end index |
Unordered collection (set) command
Add elements | sadd key member1 member2 ... |
Delete the specified element | srem key member1 member2 ... |
Return all the elements | smembers key |
Ordered set (zset) command
D: Each element is associated with a type of double Score, it indicates a weight by weight of the element small to large
Adding elements | zadd key score1 member1 score2 member2 ... |
Removing elements | zrem key member1 member2 ... |
Delete weights of the elements in the specified range | zremrangebyscore key min max |
Gets the specified range of elements | zrange key starting index ending index (negative support) |
Get score elements within the specified range | zrangebyscore key min max |
Gets score value of the element | zscore key member |
Objects (hash) commands
Providing a single property | hset key field value |
Set multiple properties | hmset key field1 value1 field2 value2 ... |
Get all the attributes specified key | hkeys key |
Gets the value of a property | hget key field |
Values of a plurality of attributes | hmget key field1 field2 ... |
Get all the value of the property | whales key |
Delete key hash | hdel key field1 field2 ... |
Get the number of attributes hash | hlen key |
Redis&python交互
U:①进入虚拟环境,安装redis包:pip install redis
②导包:from redis import *
③创建StrictRedis对象:sr = StrictRedis() ---默认主机和端口,如果不同需关键字参数指定。添加decoderesponse = True可以获取redis数据无需解码,否则是二进制数据
④增:rs.set('aa',11) 返回值布尔类型,如果保存成功返回True,否则False
删:re.delete('bb') 返回值是删除键值对的个数
改:re.set('aa',22) aa存在即修改,返回布尔类型
查:re.get('aa') 返回值是aa的值
Redis&Django交互
U:①安装django-redis包:pip install django-redis==4.8.0
②在django项目settings设置CACHES选项:
CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379/3", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "PASSWORD": "" # Redis密码,默认为空 } } }
③视图函数:
连接Redis数据库,返回一个StrictRedis对象,default表示上面配置
strict_redis=get_redis_connection('default'),可以不写,默认default
通过redis保存session数据
U:①安装包: pip install django-redis-sessions==0.5.6
②配置settings文件:
# 使用redis保存session数据 SESSION_ENGINE = 'redis_sessions.session' SESSION_REDIS_HOST = 'localhost' SESSION_REDIS_PORT = 6379 SESSION_REDIS_DB = 2 SESSION_REDIS_PASSWORD = '' SESSION_REDIS_PREFIX = 'session'
更多技术资讯可关注:gzitcast