Redis数据类型学习
Redis数据类型学习 Redis是一款开源,高性能键-值存储(key-value store).它的键值可以包括字符串(strings)类型,同时它还包括哈希(hashes),列表(lists),集合(sets)等数据类型.对于这些数据类型,可以执行原子操作.例如:对字符串进行附加操作(append);递增哈希中
Redis数据类型学习
Redis是一款开源,高性能键-值存储(key-value store).它的键值可以包括字符串(strings)类型,同时它还包括哈希(hashes),列表(lists),集合(sets)等数据类型.对于这些数据类型,可以执行原子操作.例如:对字符串进行附加操作(append);递增哈希中的值;向列表中增加元素;计算集合的交集,并集与差集等.
经常有人拿memcached和redis做比较.下面简单看下他们之间有哪些差别:
1.网络IO模型
memcached是多线程,非阻塞IO复用的网络模型.多线程模型可以发回多核作用,但有时也带来性能损耗.
redis使用单线程的IO复用模型.自己封装了一个简单的AeEvent事件处理框架,主要实现了epoll,kqueue和select,对于单纯只有IO操作来说,单线程可以将速度优势发挥到最大,但是一些操作,如排序,聚合等,单线程模型实际会严重影响整体吞吐量.
2.内存管理
memcached使用预分配的内存池的方式.能省去申请释放内存的开销,减小内存碎片产生,但这种方式也会带来一定程度上的空间浪费.数据也可能被剔除.
redis使用现场申请内存的方式来存储数据.优化内存分配,非临时数据永远不会被剔除,即便物理内存不够.这点redis更适合作为存储而不是cache
3.存储方式
memcached基本只支持简单的key-value存储,不支持枚举,不支持持久化和复制等功能.
redis除key/value之外,还支持list,set,sorted,hash等数据结构.同时还支持持久化和复制等功能.
如果希望数据不被剔除,或者需要key-value之外更多数据类型支持时,使用redis更适合.
下面安装Redis.
1.下载redis redis-2.4.14.tar.gz http://code.google.com/p/redis/downloads/list
01
root@10.1.1.45:~# ls -l redis-2.4.14.tar.gz
02
-rw-r--r-- 1 root root 627494 2013-05-08 16:16 redis-2.4.14.tar.gz
03
root@10.1.1.45:~# tar xf redis-2.4.14.tar.gz
04
root@10.1.1.45:~# cd redis-2.4.14/
05
root@10.1.1.45:redis-2.4.14# ll
06
total 84
07
-rw-rw-r-- 1 root root 12105 2012-05-23 17:32 00-RELEASENOTES
08
-rw-rw-r-- 1 root root 55 2012-05-23 17:32 BUGS
09
-rw-rw-r-- 1 root root 671 2012-05-23 17:32 CONTRIBUTING
10
-rw-rw-r-- 1 root root 1487 2012-05-23 17:32 COPYING
11
drwxrwxr-x 5 root root 4096 2012-05-23 17:32 deps
12
-rw-r--r-- 1 root root 25 2013-05-08 16:15 dump.rdb
13
-rw-rw-r-- 1 root root 30 2012-05-23 17:32 INSTALL
14
-rw-rw-r-- 1 root root 397 2012-05-23 17:32 Makefile
15
-rw-rw-r-- 1 root root 2813 2012-05-23 17:32 README
16
-rw-rw-r-- 1 root root 21094 2012-05-23 17:32 redis.conf
17
-rwxrwxr-x 1 root root 162 2012-05-23 17:32 runtest
18
drwxrwxr-x 2 root root 4096 2013-05-08 16:10 src
19
drwxrwxr-x 8 root root 4096 2012-05-23 17:32 tests
20
drwxrwxr-x 2 root root 4096 2012-05-23 17:32 utils
21
root@10.1.1.45:redis-2.4.14# make
22
root@10.1.1.45:redis-2.4.14# mkdir /etc/redis/
23
root@10.1.1.45:redis-2.4.14# cp redis.conf /etc/redis/redis.conf #拷贝配置文件
24
root@10.1.1.45:redis-2.4.14# echo "1" > /proc/sys/vm/overcommit_memory
/proc/sys/vm/overcommit_memory
可选值:0、1、2。
0,表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1,表示内核允许分配所有的物理内存,而不管当前的内存状态如何。
2,表示内核允许分配超过所有物理内存和交换空间总和的内存
这里选择1,如果不选后面make install 会提示warning.
1
root@10.1.1.45:redis-2.4.14# make install
2.启动redis服务
1
root@10.1.1.45:redis-2.4.14# redis-server /etc/redis/redis.conf
2
[11349] 08 May 20:42:05 * Server started, Redis version 2.4.14
3
[11349] 08 May 20:42:05 * DB loaded from disk: 0 seconds
4
[11349] 08 May 20:42:05 * The server is now ready to accept connections on port 6379
5
[11349] 08 May 20:42:05 - DB 0: 1 keys (0 volatile) in 4 slots HT.
6
[11349] 08 May 20:42:05 - 0 clients connected (0 slaves), 717592 bytes in use
7
[11349] 08 May 20:42:11 - DB 0: 1 keys (0 volatile) in 4 slots HT.
8
[11349] 08 May 20:42:11 - 0 clients connected (0 slaves), 717592 bytes in use
这里还需要设置redis进程为后台守护进程.
1
root@10.1.1.45:redis-2.4.14# vim /etc/redis/redis.conf
2
daemonize yes
3
root@10.1.1.45:redis-2.4.14# redis-server /etc/redis/redis.conf
4
root@10.1.1.45:redis-2.4.14# ps -ef | grep redis
5
root 10987 6834 0 17:07 pts/1 00:00:00 redis-cli
6
root 11394 1 0 20:44 ? 00:00:00 redis-server /etc/redis/redis.conf
3.测试redis
01
root@10.1.1.45:redis-2.4.14# redis-cli #redis命令操作工具
02
redis 127.0.0.1:6379> set name davehe
03
OK
04
redis 127.0.0.1:6379> get name
05
"davehe"
06
redis 127.0.0.1:6379> hset 192.168.1 test test@123 #设置1网段test用户密码
07
(integer) 1
08
redis 127.0.0.1:6379> hget 192.168.1 test #获取test值
09
"test@123"
10
redis 127.0.0.1:6379> hset 192.168.1 root root@123 #设置1网段root用户密码
11
(integer) 1
12
redis 127.0.0.1:6379> hget 192.168.1 root #获取root值
13
"root@123"
14
redis 127.0.0.1:6379> hkeys 192.168.1 #返回hash所有的field
15
1) "test"
16
2) "root"
17
redis 127.0.0.1:6379> hvals 192.168.1 #返回hash所有的value
18
1) "test@123"
19
2) "root@123"
20
redis 127.0.0.1:6379> hgetall 192.168.1 #返回hash所有filed和value
21
1) "test"
22
2) "test@123"
23
3) "root"
24
4) "root@123"
25
redis 127.0.0.1:6379> type name #查看类型 不同类型需要不同命令去获取值.
26
string
27
redis 127.0.0.1:6379> type 192.168.1
28
hash
29
redis 127.0.0.1:6379> keys * #查看所有key
30
1) "name"
31
2) "192.168.1."
4.关闭redis服务
1
root@10.1.1.45:redis-2.4.14# redis-cli shutdown
附录相关redis命令:
连接操作相关的命令
quit:关闭连接(connection)
auth:简单密码认证
持久化
save:将数据同步保存到磁盘
bgsave:将数据异步保存到磁盘
lastsave:返回上次成功将数据保存到磁盘的Unix时戳
shundown:将数据同步保存到磁盘,然后关闭服务
远程服务控制
info:提供服务器的信息和统计
monitor:实时转储收到的请求
slaveof:改变复制策略设置
config:在运行时配置Redis服务器
对value操作的命令
exists(key):确认一个key是否存在
del(key):删除一个key
type(key):返回值的类型
keys(pattern):返回满足给定pattern的所有key
randomkey:随机返回key空间的一个
keyrename(oldname, newname):重命名key
dbsize:返回当前数据库中key的数目
expire:设定一个key的活动时间(s)
ttl:获得一个key的活动时间
select(index):按索引查询
move(key, dbindex):移动当前数据库中的key到dbindex数据库
flushdb:删除当前选择数据库中的所有key
flushall:删除所有数据库中的所有key
对String操作的命令
set(key, value):给数据库中名称为key的string赋予值value
get(key):返回数据库中名称为key的string的value
getset(key, value):给名称为key的string赋予上一次的value
mget(key1, key2,…, key N):返回库中多个string的value
setnx(key, value):添加string,名称为key,值为value
setex(key, time, value):向库中添加string,设定过期时间time
mset(key N, value N):批量设置多个string的值
msetnx(key N, value N):如果所有名称为key i的string都不存在
incr(key):名称为key的string增1操作
incrby(key, integer):名称为key的string增加integer
decr(key):名称为key的string减1操作
decrby(key, integer):名称为key的string减少integer
append(key, value):名称为key的string的值附加value
substr(key, start, end):返回名称为key的string的value的子串
对List操作的命令
rpush(key, value):在名称为key的list尾添加一个值为value的元素
lpush(key, value):在名称为key的list头添加一个值为value的 元素
llen(key):返回名称为key的list的长度
lrange(key, start, end):返回名称为key的list中start至end之间的元素
ltrim(key, start, end):截取名称为key的list
lindex(key, index):返回名称为key的list中index位置的元素
lset(key, index, value):给名称为key的list中index位置的元素赋值
lrem(key, count, value):删除count个key的list中值为value的元素
lpop(key):返回并删除名称为key的list中的首元素
rpop(key):返回并删除名称为key的list中的尾元素
blpop(key1, key2,… key N, timeout):lpop命令的block版本。
brpop(key1, key2,… key N, timeout):rpop的block版本。
rpoplpush(srckey, dstkey):返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部
对Set操作的命令
sadd(key, member):向名称为key的set中添加元素member
srem(key, member) :删除名称为key的set中的元素member
spop(key) :随机返回并删除名称为key的set中一个元素
smove(srckey, dstkey, member) :移到集合元素
scard(key) :返回名称为key的set的基数
sismember(key, member) :member是否是名称为key的set的元素
sinter(key1, key2,…key N) :求交集
sinterstore(dstkey, (keys)) :求交集并将交集保存到dstkey的集合
sunion(key1, (keys)) :求并集
sunionstore(dstkey, (keys)) :求并集并将并集保存到dstkey的集合
sdiff(key1, (keys)) :求差集
sdiffstore(dstkey, (keys)) :求差集并将差集保存到dstkey的集合
smembers(key) :返回名称为key的set的所有元素
srandmember(key) :随机返回名称为key的set的一个元素
对Hash操作的命令
hset(key, field, value):向名称为key的hash中添加元素field
hget(key, field):返回名称为key的hash中field对应的value
hmget(key, (fields)):返回名称为key的hash中field i对应的value
hmset(key, (fields)):向名称为key的hash中添加元素field
hincrby(key, field, integer):将名称为key的hash中field的value增加integer
hexists(key, field):名称为key的hash中是否存在键为field的域
hdel(key, field):删除名称为key的hash中键为field的域
hlen(key):返回名称为key的hash中元素个数
hkeys(key):返回名称为key的hash中所有键
hvals(key):返回名称为key的hash中所有键对应的value
hgetall(key):返回名称为key的hash中所有的键(field)及其对应的value

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.
