Redis使用及相关

777 阅读2分钟

什么是Redis

Redis是一个开源的使用ANSI C语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

redis的下载

官网地址:http://redis.io/
下载地址:https://redis.io/download

操作工具及系统

操作工具:Xshell,Xftp 操作系统:Linux

Redis的安装

由于redis需要c语言的编译环境,,所以需要安装gcc。如下命令:

[root@localhost ~]# yum -y install gcc-c++

查询是否安装GCC:

[root@localhost ~]# gcc

出现 :gcc: no input files 表示安装成功。 安装步骤: 1.通过Xftp工具将将redis的源码包上传到linux系统。 2.解压缩redis的源码包。 3.打开解压后的目录 输入命令:make 4. 输入命令:make install PREFIX=/usr/local/redis进行安装 PREFIX 必须是大写的。 5.查看/usr/local/redis/bin路径是否存在,存在即安装成功

连接redis

前台启动redis

cd 到/usr/local/redis/bin目录下,输入

[root@localhost bin]# ./redis-server 

出现如图

image.png

后台启动redis

1.把/root/redis-3.0.0/redis.conf复制到/usr/local/redis/bin目录下

	[root@localhost redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
  1. 打开/usr/local/redis/bin目录
    image.png

3..vim打开修改redis.conf

image.png
4.搜索daemonize 命令: /daemonize ,将daemonize no修改为daemonize yes .

insert新插入操作,:wq退出

image.png

5.启动Redis

	[root@localhost bin]# ./redis-server redis.conf

4.查看redis是否开启:

	[root@localhost bin]# ps -ef|grep redis

###连接与断开 输入./redis-cli 连接redis

image.png
使用快捷键CTRL + C退出链连接,或者命令quit,exit

Redis数据类型

Redis支持的键值数据类型有5种。如下: 字符串类型 (String) 散列类型(hash) 列表类型(List) 集合类型(set) 有序集合类型(SortedSet)

字符串类型 (String)

采用key-value键值对方式,key注意区分大小写 set key value 设置值

image.png

get key 获取值

image.png

incr key 加一

image.png

decr key 减一

image.png

散列类型(hash)

采用key-field-value方式

hset key field value 设置值

image.png

hget key field 获取值

image.png

hincrby key field 正父负数 设置增数量

image.png

List

lrange list 0 -1 0 到 -1 查看全部

lpush list value1 value2 从左添加,插在最前面

image.png
rpush list value1 value2 从右添加,插入到最后面
image.png

lpop list 删除最前的

image.png

rpop list 删除最后面的

image.png

Set 无序不重复的

sadd set1 value1 value2 添加

image.png

smembers set1 (查询

image.png

srem set1 value1 删除

image.png

单机版连接

未使用连接池

创建jedis对象,获取对应值

  @Test
public void testConnetctJedis() throws Exception {
	Jedis jedis = new Jedis("192.168.1.77", 6379);
	String s= jedis.get("hello");
	System.out.println(s);
	jedis.close();
}

###使用连接池 获取连接池,从连接池中回去jedis对象,进行操作 @Test public void testConnetctJedisPool() throws Exception {

	JedisPool jedisPool = new JedisPool("192.168.1.77", 6379);
	Jedis jedis = jedisPool.getResource();
	jedis.set("hello", "world");
	String s= jedis.get("hello");
	System.out.println(s);
	jedis.close();
	jedisPool.close();
}

集群版连接

获取JedisCluster对象进行操作

  @Test
public void testJedisCluster() throws Exception {
	Set<HostAndPort> nodes = new HashSet<>();
	nodes.add(new HostAndPort("192.168.1.77", 7001));
	nodes.add(new HostAndPort("192.168.1.77", 7002));
	nodes.add(new HostAndPort("192.168.1.77", 7003));
	nodes.add(new HostAndPort("192.168.1.77", 7004));
	nodes.add(new HostAndPort("192.168.1.77", 7005));
	nodes.add(new HostAndPort("192.168.1.77", 7006));
	JedisCluster jedisCluster = new JedisCluster(nodes);
	jedisCluster.set("hello", "world");
	String result = jedisCluster.get("hello");
	System.out.println(result);
	jedisCluster.close();
}