Redis的安装与持久化

本文主要是对于redis安装与持久化中非常浅显的知识进行介绍,并不深入了解,只接触表面,对一些较复杂的内容也不过多描述。如文中有错误之处,望不吝赐教,谢谢~

一、Redis简介

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

Redis是一款高性能的NOSQL系列的非关系型数据库,redis在Java开发中主要用来存储缓存用的数据以及在需要高速读/写的场合使用它快速读/写。

NoSQL技术,这是一种基于内存的数据库,并且提供一定的持久化功能。
Redis和MongoDB是当前使用最广泛的NoSQL,而就Redis技术而言,它的性能十分优越,可以支持每秒十几万此的读/写操作,其性能远超数据库,并且还支持集群、分布式、主从同步等配置,十分强大。

在一般情况下,不区分redis技术和redis数据库,都称作redis。
redis作为一种非关系型数据库,其是基于内存,不同于关系数据库基于硬盘,其速度远远大于关系数据库。但是由于内存的局限性,所以对何种数据何时使用redis我们需要做出取舍。
一般对于读操作较多且数据量较小的情况可考虑使用redis,例如“好友列表查询”“排行榜”等等。

二、安装redis

(1)进入github-redis下载redis压缩包

在这里插入图片描述
(2)解压下载下来的redis-64-3.0.503.zip ,直接打开便可使用。(其实这是绿色版本,不用安装,下载下来解压就好)。

  • redis.windows.conf:配置文件
  • redis-cli.exe:redis的客户端
  • redis-server.exe:redis服务器端

在这里插入图片描述

一般启动好服务器端再打开客户端就可以使用redis了。

三、redis持久化

由于redis的数据存储在内存中,不便于保存(当服务器和客户端关闭或电脑关机时,数据会丢失),为了将redis中的数据保存在硬盘中,便需要持久化技术。
redis持久化分为两种方式一种是RDB(默认方式),一种是AOF方式。

(1)RDB

redis.windows.conf配置文件中,有专门对RDB持久化的配置。

################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   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

*“sava 900 1”*表示900秒内至少有一个键值发生改变,就执行持久化操作一次,相应的我们可以根据需要自行修改持久化条件。
每次持久化后的rdb文件会保存在redis的解压目录下。

(2)AOF:日志记录的方式,可以记录每一次命令的操作并持久化

redis.windows.conf配置文件中,有专门对AOF持久化的配置。

############################## APPEND ONLY MODE ###############################

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# 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

"appendonly no"表示默认AOF方式是关闭的,若要打开,将no改为yes即可。

# 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.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".

# appendfsync always
appendfsync everysec
# appendfsync no

"appendfsync always"表示每一次操作都进行持久化,“appendfsync everysec”表示每隔一秒进行一次持久化操作(默认是这样),“appendfsync no”表示不进行持久化操作。
每次持久化后的aof文件会保存在redis的解压目录下。

在这里插入图片描述
2020.03.07

发布了52 篇原创文章 · 获赞 59 · 访问量 6814

猜你喜欢

转载自blog.csdn.net/ataraxy_/article/details/104711788
今日推荐