Redis

Redis的概念

Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

redis是一个将数据存储在内存中的非关系型数据库。它存储值的形式以键值的形式存储。

Redis的数据结构

存储形式:Key-Value

Key:都是字符串

value:
1.字符串类型string
2.哈希类型hash
3.列表类型list
4.集合类型set
5.有序集合类型sortedset

Redis的命令

  • 字符串
    1.set key value
    2.get key
    3.del key

  • 哈希
    1.hset key field value
    2.hget key field
    3.hgetall
    4.hdel key field

  • 列表
    1.lpush key value
    2.rpush key value
    3.lrange key start end
    4.lpop key
    5.rpop key

  • 集合
    1.sadd key value
    2.smembers key
    3.srem key value

  • 有序集合
    1.zadd key score value
    2.zrange key start end (with scores)
    3.zrem key value

  • 通用命令
    1.keys *
    2.type [key]
    3.del key

Redis的持久化

redis是一个内存数据库,当redis服务器重启,数据会丢失。我们可以将redis内存中的数据持久化保存到硬盘中。下面说一下持久化的机制。

持久化有两种机制。即ROB和AOF。

1.RDB:默认方式,不需要进行配置,默认就使用机制。在一定的时间间隔中,检测key的变化,然后持久化数据。
以windows为例。
在redis.windows.conf文件中的配置。
在这里插入图片描述

#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

图中的注释部分说的很明确。目前的配置是900s至少一个key的value改变,300秒至少10个key的value改变,60s至少10000个key的value改变,就会进行一次持久化。会生成一个后缀是.rdb的文件来储存数据。

需注意的是,配置之后如果需要进行持久化,启动redis服务器的时候需要以redis.windows.conf的形式启动。
在cmd命令启动。

redis-server.exe redis.windows.conf

2.AOF:日志记录的方式,可以记录每一条命令的操作。可以在每次的操作后,持久化数据。
同样在redis.windows.conf配置文件中。
在这里插入图片描述

# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly no

# The name of the append only file (default: "appendonly.aof")
appendfilename "appendonly.aof"

# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#

把appendonly no改为appendonly yes即可开启AOF的持久化。会生成一个以.aof为后缀的文件来储存数据。
需要注意上面的开关配置。

# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.

no:不进行持久化
always:每一次操作都进行持久化
everysec:每隔一秒都进行持久化

把对应的开关开启即可。(去掉前面的#注释符)

总结redis持久化
RDB:性能好,容易丢数据。
AOF:性能不好,不容易丢数据。

Jedis的概念

jedis是一款java操作redis的数据库工具。
在java使用时添加相应的jar包即可。

一般jedis操作redis的步骤:
1.获取连接

Jedis jedis = new jedis("localhost",port);

2.操作数据库
相关的操作方法可以查看相应的api。
3.关闭连接

jedis,close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值