1, Redis memory database
Background
With the advent of the Internet + big data era, the traditional relational database can no longer large sites in the growing volume of data traffic and meet. This time we need a way to quickly access the component data to ease the pressure on the database server I / O, and to solve the bottleneck on system performance.
History of the development of the database
-
1. Before the Internet + big data era, some internal corporate information management system, a single database instance will be able to meet the needs of the system
single database instance -
2. With the increase of the user's system access, increase in the amount of data, a single instance of the database system has failed to meet the needs of the read
buffer (memcache) + single database instance -
3. The read cache can relieve pressure of the system, but the amount of data written to the pressure continues to increase;
buffer + + database read from the main separator -
4. The data amount increases again, after the separate read and write, write library pressure of the main bottlenecks database;
cache cluster database from the master + + + sub-library separate read and write sub-table -
5. Internet + big data era, relational databases can not access some of the very good high concurrency, high real-time, and the data format is not fixed data. + + NoSQL separate read and write from the main + sub-library database cluster sub-table
2 Redis
Redis is a high-performance, open source, C language development, key nosql database for storing data.
NoSQL: not only sql, refers to non-relational database Redis / MongoDB / Hbase Hadoop
relational databases: MySQL, oracle, SqlServer
Redis properties
- When persistence Redis support data, data in memory can be saved to disk, restart can be loaded again using
- Redis supports not only simple key-value data types, while also providing List, set data types
- Redis supports backup data
Redis what's the use?
The main role of the Redis: Fast access
Redis application scenarios
online buddy list thumbs up / spike / broadcast platform / product ranking / single sign-on
Redis how to use?
Official website address: official website
Command Address: Command Address
Redis five data types and scenarios
string / list / set / hash / zset
Redis installation and startup
in Ubuntu system
sudo apt-get install redis-server
View Help command
redis-server --help
Redis edit the configuration file
sudo vim /etc/redis/redis.conf
will daemonize no change daemonize yes
to save and exit
Start
redis-server
Open the service sudo service redis start
Close service sudo service redis stop
Redis open processes in Windows system : directly extracted, the windows do not support daemonize
Redis's profile
/etc/redis/redis.conf
When redis run as a daemon, it will write a pid file to /var/run/redis.pid inside.
daemonize no
Listening port number, the default is 6379, if you set to 0, redis will listen to any client is not connected to the socket.
port 6379
Set number of databases.
databases 16
The number of times a given time interval and save the write data to disk
The following example is meant:
if at least one key value change, then stored within 900 seconds
value change key 10 if there are at least 300 seconds, the stored
60 seconds if at least 10,000 key value change, the stored
save 900. 1
save 300 10
save 60 10000
Listening port number, the default is 6379, if you set to 0, redis will listen to any client is not connected to the socket.
port 6379
Redis allows only local connections by default, does not allow other machines connected to
bind 127.0.0.1
More profile: https: //www.cnblogs.com/kreo/p/4423362.html
Simple to use Redis database
DBSIZE ------------------------- view the current key number of databases
keys * ---------------- ----------- view the key content
FLUSHDB --------------------- clear the current number of key database
fLUSHALL ----- --------------- clear all libraries key (caution)
EXISTS Key ----------------------- judge whether there are key
3, redis five common types of data
1.redis-string
redis string is the most basic type, a key corresponding to a value
string can contain any data, not exceed the maximum 512M
set/get/del/append/strlen
set ---- 设置值
get ---- 获取值
mset ---- 设置多个值
mget ---- 获取多个值
append ---- 添加字段
del ---- 删除
strlen ---- 返回字符串长度
incr/decr/incrby/decrby
incr ---- 增加
decr ---- 减少
incrby ----- 制定增加多少
decrby ----- 制定减少多少
getrange / setrange
getrange ---- 获取指定区间范围内的值,类似between....and的关系
setrange ---- 代表从第几位开始替换,下脚本从零开始从0 -1表示全部
2.redis-list (a single value of the multi value)
List (list)
list is a simple list of strings, insertion order sort, the elements can add a head of the list (left) or a tail portion (right side)
which is a list of the actual underlying
Lpus / Rpus / Lrange
lpush/rpush/lrange ---- 从左/从右/获取指定长度
lpush list01 1 2 3 4 5 倒序排列
rpush list02 1 2 3 4 5 正序排列
lrange list01 0 -1 获取list01 中的所有值
lpop/rpop
lpop/rpop ---- 移除最左/最右
lpop list01 删除最左元素
rpop list01 删除最右元素
lindex, obtained according to the indexing subscript element (top to bottom)
lrange list01 0 -1
lindex list01 1
llen, seeking list length
llen list01
lrem key
删N个value
lrem list01 2 1 在list01中删除2个1
ltrim key
ltrim ---- 开始index结束index,截取指定范围的值后在赋值给key
ltrim list01 0 2 截取list01 从0到2的数据在赋值给list01
rpoplpush list1 list2 to list1 last pushed first in list2
lrange list01 0 -1
lrange list02 0 -1
rpoplpush list1 list2
lset key index value
lset list01 0 x 将list02中第一位换成x
linsert key before/after
linsert list01b before x php 在x之前加字段php
3.redis-Hash
is a collection of hash key
hash is a string type and the value of field mapping table, hash particularly suitable for storing objects
hset/hget/hmset/hmget/hgetall/hdel
设值/取值/设值多个值/取多个值/取全部值/删除值
hset user id 11
hget user id
hmset customer id 11 name juran age 26
hmget customer id name age 只返回相应的值
hgetall customer 返回全部
hdel user id 删除id
hlen seeking hash length
hlen customer
hexists key
hexists ---- 在key里面的某个值
存在返回1 ,不存在返回0
HKEY / whale
hkeys students
hvals students
4.redis-set (not duplicate)
Set (set)
SET unordered collection of type string, no duplicate elements
Sadd / smembers / sismember
sadd/smembers/sismember ---- 添加/查看集合/查看是否存在
sadd set01 1 2 2 3 3 去掉重复添加
smembers set01 得到set01
sismember set01 1 如果存在返回1 不存在返回0
scard
scard ---- 获取集合里面的元素个数
scard set01
srem key value
srem ---- 删除集合中元素
srem set01 3
SMEMBERS set01 3已经被删除掉
srandmember key
srandmembe ---- 随机出几个数
sadd set02 1 2 3 4 5 6 7 8
srandmember set02 2
spop key
spop ---- 随机出栈
spop set01
smove key1 key2
sadd set03 x y z
smove set01 set03 2 将set01中的2 移动到set03中
Mathematical collections
sadd set01 1 2 3 4 5
sadd set02 1 2 3 a b
差集
SDIFF set01 set02 返回 4 5 在第一个set中不在第二个set中
交集
SINTER set01 set02 返回 1 2 3
并集
SUNION set01 set02 返回set01 set02 中的值 去掉重复
5.redis-Zset
Zset (ordered set)
zadd/zrange
zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5
zrange zset01 0 -1
带分数返回 withscores
zrangebyscore key start end
zrangebyscore key start end----根据开始结束来取值
zrangebyscore zset01 60 70
zrangebyscore zset01 60 (90 表示不包含90
zrangebyscore zset01 60 90 limit 1 2 从第一条开始截取2条
zrem key
zrem key value---- 某score下对应的value值,作用是删除元素
zrem zset01 v1
zcard/zcount key score 区间/zrank key values
zcard 求zset01 总条数
zcount zset01 60 90 求60-90个数
zrank zset01 v2 返回1 返回对应下角标,从0开始
4, Python operation Redis
Installation and connection redispy
Installation
pip install redis
connection
r = redis.StrictRedis(host='localhost',port=6379,db=0)
String related operations
import redis
class TestString(object):
def __init__(self):
self.r = redis.StrictRedis(host='127.0.0.1', port=6379)
# 设置值
def string_set(self, keys, items):
res = self.r.set(keys, items)
print(res) # 返回的是布尔类型的值
# 取值
def string_get(self, keys):
res = self.r.get(keys)
return res
# 设置多个值
def string_mset(self, items):
if isinstance(items, dict):
res = self.r.mset(items)
print(res)
# 取多个值
def string_mget(self, keys):
if isinstance(keys, dict):
res = self.r.mget(keys)
print(res)
# 删除
def string_del(self, keys):
if self.r.exists(keys):
self.r.delete(keys)
else:
# return "{} is not found".format(keys)
raise ValueError("{} is not found".format(keys))
if __name__ == '__main__':
s = TestString()
d = {
"user": "123",
"user1": "1"
}
s.string_mset(d)
print(s.string_del("user"))
A list of related operations
import redis
class List_Redis(object):
def __init__(self):
self.r = redis.StrictRedis(host='127.0.0.1', port=6379)
# 添加值
def list_lpush(self, keys, items):
res = self.r.lpush(keys, items)
return res
# 删除元素
def list_lpop(self, keys):
result = self.r.lpop(keys)
return result
if __name__ == '__main__':
l = List_Redis()
# l.list_lpush('li9', '1 2 3 4 5 6 7 8 8 9')
l.list_lpop("li9")
A collection of related operations
import redis
class Set_Redis(object):
def __init__(self):
self.r = redis.StrictRedis(host='127.0.0.1', port=6379)
# 添加数据
def set_sadd(self,keys, items):
result = self.r.sadd(keys, items)
return result
# 删除数据
def set_srem(self, keys, values):
res = self.r.srem(keys, values)
return res
# 随机删除元素
def set_pop(self, keys):
res = self.r.spop(keys)
return res
if __name__ == '__main__':
s = Set_Redis()
s.set_sadd('s1', '1 2 3 4 5')
s.set_pop('s1')
Hash-related operations
import redis
class Hash_Redis(object):
def __init__(self):
self.r = redis.StrictRedis(host='127.0.0.1', port=6379)
# 设置值
def hash_hset(self, keys, items,values):
result = self.r.hset(keys, items, values)
return result
# 取值
def hash_hgetall(self, keys):
result = self.r.hgetall(keys)
return result
# 判断是否存在 存在返回1 不存在返回0
def hash_hexists(self, keys, values):
res = self.r.hexists(keys, values)
print(res)
if __name__ == '__main__':
h = Hash_Redis()
h.hash_hset('h1', 'id', '1')
5, Redis master-slave configuration
- ⼀ a master can have more than one slave, a slave ⼀ separate warranty may have more than one slave, it goes on, forming a strong zoomed multi-level server cluster architecture
- master used to write data, slave to read data, the statistics: literacy ratio site is 10: 1
- The configuration may be implemented by separate read and write from the primary
- master and slave is a redis example (redis service)
Master-slave configuration
configure the master
Modify etc / redis / redis.conf file
bind 0.0.0.0 or change to a native IP
Configuration from
Copy etc / redis / redis.conf file
cp redis.conf slave.conf
Modify redis / slave.conf file
vim slave.conf
bind 192.168.154.131 (host the IP)
slaveof 192.168.154.131 (host IP) 6379 (host port)
Port 6378 (slave port)
Turn on the host service
src/redis-server slave.conf
src/redis-cli -h 0.0.0.0 -p 6379
Enter from the client
src/redis-cli -h 192.168.154.131 -p 6378