学习笔记之Redis

Redis logo

简介

什么是Redis?我相信,只要你去翻一翻与Redis相关的网站就会发现它们的回答都差不多。要知道它们在国内可都是权威啊!我在这就不去使用CV大法来班门弄斧了。简单谈谈我的理解,当被人问到这个问题时,我脑子里想的就是这些:

数据类型

Redis支持五种数据类型:

  • string(字符串)
    Redis最基本的数据类型;
    可包含任何数据,包括图片,序列化的对象;
    一个键最大能存储512MB;

  • hash(哈希)

  • list(列表)
    每个子元素都是string类型的双向列表;

  • set(集合)

  • zset(sorted set:有序集合)

存储机制

为了将读写速度达到最快,Redis将数据都先读到内存中,再异步将数据写入磁盘。
当设置了Redis的最大使用内存时,在数据达到内存阈值后就不能再继续插入新值。

分布模式

Redis使用Master-Slave模式。Master节点会单向同步数据至Slave节点,而Slave节点在启动时会连接Master节点来同步数据。我们利用Master节点插入数据,Slave节点检索数据,读写分离,降低了单个机器访问频率过高而宕机的概率。

数据操作

Redis命令学习:https://www.redis.net.cn/tutorial/3506.html

平台搭建

各平台安装Redis教程链接:https://www.redis.net.cn/tutorial/3503.html
下面说说我自己在搭建Centos8平台下的Redis环境中需要注意的一些细节:

  • 在未修改配置文件前,带配置文件后台启动Redis时,&得放在最后面
redis-server路径 redis.conf路径 & #未修改配置文件前后台启动Redis
  • 配置文件修改
    redis.conf

# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# 默认为no,改成yes后启用Redis服务时默认后台启用
daemonize yes


# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
# Redis默认16个数据库,初次使用下标为0的那个
databases 16

#备注:取消注释后,redis服务器只能在远程客户端访问,本机无法访问,一般要注释掉
#bind 127.0.0.1

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
#    "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
# 如果protected-mode为yes,会导致Jedis无法连接
protected-mode no


Jedis

开始使用Jedis之前,我们需要确保已经安装了 redis 服务及 Java redis 驱动。
Jedis驱动依赖下载地址:https://github.com/redis/jedis
在测试类中我们只需要一行代码

Jedis jedis = new Jedis("IP",端口号);

获得Jedis对象后,其余所有操作和Redis操作别无二致,所以我们学习的重点还是应该放在Redis语句的使用和文件的配置上。

结语

作者目前对Redis的掌握只停留在对概念的理解,基本命令的使用还有Jedis的基本操作。
相对其他NoSQL数据库而言,Redis对新手还是很友好的,参考资料也比较多,安装配置也不繁琐,很容易上手。
本文整理了学习Redis所需的大多网址,希望在大家学习Redis的时候能有所帮助。
等作者日后对Redis有了更深层的掌握后会回更的。

(完)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容