Redis学习02之String & Database
Redis支持5种数据类型,这里记录第一个String字符,Redis中的字符串和其它编程语言或键值库功能相似,函数丰富方便,字符串的值可以存储3种类型的值:字节串、整数、浮点数。KEY可以是数字、大小写字母、下划线或中文, Value 有空格要以引号括起。如果value是整数或符点数可以对其自增、减操作。可以增、删、改、查等操作, KEY 和Value可以是中文,但是key是中文是使用keys 列出键时中文为unicode码。
String SET/GET
127.0.0.1:6379> ping PONG 127.0.0.1:6379> set 1 a OK 127.0.0.1:6379> get 1 "a" 127.0.0.1:6379> set keyname valuename OK 127.0.0.1:6379> get keyname "valuename" 127.0.0.1:6379> set name "weejar zhang" OK 127.0.0.1:6379> get name "weejar zhang"
如果KEY存在则不更新
127.0.0.1:6379> get name "weejar zhang blog .com" 127.0.0.1:6379> set name "anbob.com" NX (nil) 127.0.0.1:6379> get name "weejar zhang blog .com"
多个KEY Mget
127.0.0.1:6379> keys * 1) "blog" 2) "name" 127.0.0.1:6379> mget name blog 1) "weejar" 2) "anbob.com" 127.0.0.1:6379> getrange blog 4 -1 "b.com" 127.0.0.1:6379> getrange blog 4 -2 "b.co"
整数、浮点加减
127.0.0.1:6379> set age 30 OK 127.0.0.1:6379> get age "30" 127.0.0.1:6379> incr age (integer) 31 127.0.0.1:6379> incrby age 3 (integer) 34 127.0.0.1:6379> decr age (integer) 33
检索模糊
127.0.0.1:6379> set 名字 张维照 OK 127.0.0.1:6379> get 名字 "\xe5\xbc\xa0\xe7\xbb\xb4\xe7\x85\xa7" 127.0.0.1:6379> keys * 1) "1" 2) "keyname" 3) "name" 4) "age" 5) "\xe5\x90\x8d\xe5\xad\x97" 127.0.0.1:6379> keys name 1) "name" 127.0.0.1:6379> keys na (empty array) 127.0.0.1:6379> keys na* 1) "name" 127.0.0.1:6379> keys na?e 1) "name" 127.0.0.1:6379> keys nam[ed] 1) "name" 127.0.0.1:6379> type name string 127.0.0.1:6379> type age string 127.0.0.1:6379> exists name (integer) 1 127.0.0.1:6379> keys 1 1) "1"
改名和删除
127.0.0.1:6379> rename 1 key1 OK 127.0.0.1:6379> del key1 (integer) 1 127.0.0.1:6379> keys * 1) "keyname" 2) "name" 3) "age" 4) "\xe5\x90\x8d\xe5\xad\x97" 127.0.0.1:6379> flushdb OK 127.0.0.1:6379> keys * (empty array) 清空所有 127.0.0.1:6379> flushall
追加和截取
127.0.0.1:6379> set name "weejar zhang" OK 127.0.0.1:6379> get name "weejar zhang" 127.0.0.1:6379> append name " anbob.com" (integer) 22 127.0.0.1:6379> get name "weejar zhang anbob.com" 127.0.0.1:6379> getrange name 13 17 "anbob" 127.0.0.1:6379> setrange name 13 "blog " (integer) 22 127.0.0.1:6379> get name "weejar zhang blog .com" 127.0.0.1:6379> get name "anbob.com" 127.0.0.1:6379> getrange blog 4 -1 "b.com" 127.0.0.1:6379> getrange blog 4 -2 "b.co"
getrange在是从原来的substr改名,在python redis中依旧保留两种。
大小写互换
127.0.0.1:6379> set chr h OK 127.0.0.1:6379> get chr "h" 127.0.0.1:6379> setbit chr 2 0 (integer) 1 127.0.0.1:6379> get chr "H" 127.0.0.1:6379> setbit chr 2 1 (integer) 0 127.0.0.1:6379> get chr "h"
生命周期
127.0.0.1:6379> get name "weejar zhang blog .com" 127.0.0.1:6379> ttl name (integer) -1 127.0.0.1:6379> expire name 10 (integer) 1 127.0.0.1:6379> ttl name (integer) 8 127.0.0.1:6379> ttl name (integer) 7 127.0.0.1:6379> ttl name (integer) 6 127.0.0.1:6379> ttl name (integer) 5 127.0.0.1:6379> ttl name (integer) -2 127.0.0.1:6379> get name (nil) 127.0.0.1:6379> set name "anbob.com" EX 10 OK 127.0.0.1:6379> ttl name (integer) 9 127.0.0.1:6379> ttl name (integer) 7
改为永久
127.0.0.1:6379> persist name (integer) 1 127.0.0.1:6379> ttl name (integer) -1
随机KEY
127.0.0.1:6379> set age 30 OK 127.0.0.1:6379> keys * 1) "age" 2) "name" 127.0.0.1:6379> randomkey "age" 127.0.0.1:6379> randomkey "age" 127.0.0.1:6379> randomkey "age" 127.0.0.1:6379> randomkey "name" 127.0.0.1:6379> randomkey "name"
获取并更新
127.0.0.1:6379> set next 30 OK 127.0.0.1:6379> get next "30" 127.0.0.1:6379> getset next 31 "30" 127.0.0.1:6379> get next "31"
数据库
127.0.0.1:6379> config get databases 1) "databases" 2) "16" 127.0.0.1:6379> info keyspace # Keyspace db0:keys=2,expires=0,avg_ttl=0
KEY移动数据库
127.0.0.1:6379> move age 2 (integer) 1 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> select 2 OK 127.0.0.1:6379[2]> keys * 1) "age" 127.0.0.1:6379[2]> info keyspace # Keyspace db0:keys=1,expires=0,avg_ttl=0 db2:keys=1,expires=0,avg_ttl=0
Tip:
Redis 使用 DB number 实现类似关系型数据库中 schema 的功能。不同 DB number 表示的数据库是隔离的,但是目前只能使用数字来表示一个数据库
redis-cli -n dbnumber redis://127.0.0.1:6379/dbnumber
python 调用
[root@MiWiFi-R2100-srv ~]# python -m pip install redis Collecting redis Downloading https://files.pythonhosted.org/packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl (72kB) 100% |████████████████████████████████| 81kB 13kB/s Installing collected packages: redis [root@MiWiFi-R2100-srv ~]# python Python 2.7.5 (default, Nov 20 2015, 02:00:19) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import redis >>> client=redis.Redis() >>> client.get('name') 'weejar zhang' >>> client.get('age') '33' >>> client.get('名字') '\xe5\xbc\xa0\xe7\xbb\xb4\xe7\x85\xa7' >>> client.get('名字').decode >>> print(client.keys()) ['keyname', 'name', 'age', '\xe5\x90\x8d\xe5\xad\x97'] >>> for key in client.keys(): ... print(key.decode()) ... keyname name age Traceback (most recent call last): File "", line 2, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128) >>> import sys >>> reload(sys) <module 'sys' (built-in)> >>> sys.setdefaultencoding('utf8') >>> for key in client.keys(): ... print(key.decode()) ... keyname name age 名字 >>> for key in client.keys(): ... print(client.get(key)) ... valuename weejar zhang 33 张维照
对不起,这篇文章暂时关闭评论。