1.Redis 下载与安装
- Windows版下载地址: https://github.com/microsoftarchive/redis/releases
- Linux版下载地址: https://download.redis.io/releases/
2.在Linux系统上安装Redis
-
将Redis安装包上传到Linux系统
-
解压安装包
-
安装Redis的依赖环境gcc 命令:yum install -y gcc-c++
-
进入/usr/local/redis-4.0.0,进行编译 命令:make
-
进入Redis的src目录 进行安装 命令:make install
3.在windows上安装Redis
直接解压
4.在Linux上启动redis服务
在redis-4.0.0/src/ 目录下 ./redis-server
客户端连接
修改文件 redis-4.0.0/redis.conf => daemonize no -> yes
后台运行redis服务 => /src/redis-server ./redis.conf
5.在windows上启动redis服务
6.配置redis服务密码
指定密码为123456 => vim redis.conf => requirepass 123456
登陆时认证 => ./src/redis-cli -h localhost -p 6379 -a 123456
7.通过windows远程连接redis服务(关闭防火墙/打开端口)
开启远程连接 => vim redis.conf => # bind 127.0.0.1
windows远程访问redis
8.Redis常用命令(更多命令参考 https://www.redis.net.cn)
- 字符串类型操作命令
SET key value 设置指定key的值
GET key 获取指定key的值
SETEX key seconds value 设置指定key的值,并将key的过期时间设为seconds秒
SETNX key value 只有在key不存在时设置key的值
- 哈希类型操作命令
HSET key field value 将哈希表 key中的字段field的值设为value
HGET key field 获取存储在哈希表中指定字段的值
HDEL key field 删除存储在哈希表中的指定字段
HKEYS key 获取哈希表中所有字段
HVALS key 获取哈希表中所有值
HGETALL key 获取在哈希表中指定key 的所有字段和值
- 列表类型操作命令
LPUSH key value1 [value2] 将一个或多个值插入到列表头部
LRANGE key start stop 获取列表指定范围内的元素
RPOP key 移除并获取列表最后一个元素
LLEN key 获取列表长度
BRPOP key1 [key2] timeout 移出并获取列表的最后一个元素,
如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止
- 集合类型&有序集合类型操作命令
// 无序集合
SADD key member1 [member2] 向集合添加一个或多个成员
SMEMBERS key 返回集合中的所有成员
SCARD key 获取集合的成员数
SINTER key1 [key2] 返回给定所有集合的交集
SUNION key1 [key2] 返回所有给定集合的并集
SDIFF key1 [key2] 返回给定所有集合的差集
SREM key member1 [member2] 移除集合中一个或多个成员
// 有序集合
ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员
或者更新已存在成员的分数
ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合中指定区间内的成员
ZINCRBY key increment member 有序集合中对指定成员的分数加上增量increment
ZREM key member [member ...] 移除有序集合中的一个或多个成员
- 通用命令
KEYS pattern 查找所有符合给定模式( pattern)的key
EXISTS key 检查给定key是否存在
TYPE key 返回key所储存的值的类型
TTL key 返回给定 key的剩余生存时间(TTL, time to live),以秒为单位
DEL key 该命令用于在 key存在是删除key
9.在Java中操作Redis(使用Spring Data Redis)
- 导入maven坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 配置application.yml
spring:
# Redis相关配置
redis:
host: localhost
port: 6379
#password: 123456
database: 0 #操作的是0号数据库
jedis:
# Redis连接池配置
pool:
max-active: 8 #最大连接数
max-wait: 1ms #连接池最大阻塞等待时间
max-idle: 4 #连接池中的最大空闲连接
min-idle: 0 #连接池中的最小空闲连接
- 创建配置类(解决序列化问题) =>RedisConfig
package com.shryu.config;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Redis配置类
*/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
//默认的Key序列化器为:JdkSerializationRedisSerializer
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
- SpringDataRedisTest
package com.shryu;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@SpringBootTest
public class SpringDataRedisTest {
@Autowired
private RedisTemplate redisTemplate;
/**
* 操作String类型数据
*/
@Test
public void testString(){
redisTemplate.opsForValue().set("city123","beijing");
String value = (String) redisTemplate.opsForValue().get("city123");
System.out.println(value);
redisTemplate.opsForValue().set("key1","value1",10L, TimeUnit.SECONDS);
Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent("city123", "nanjing");
System.out.println(aBoolean);
}
/**
* 操作Hash类型数据
*/
@Test
public void testHash(){
HashOperations hashOperations = redisTemplate.opsForHash();
// 存值
hashOperations.put("002","name","xiaoming");
hashOperations.put("002","age","20");
hashOperations.put("002","address","bj");
// 取值
String age = (String) hashOperations.get("002", "age");
System.out.println(age);
// 获得hash结构中所有字段
Set keys = hashOperations.keys("002");
for (Object key : keys) {
System.out.println(key);
}
// 获得hash结构中所有值
List values = hashOperations.values("002");
for (Object value : values) {
System.out.println(value);
}
}
/**
* 操作List类型数据
*/
@Test
public void testList(){
ListOperations listOperations = redisTemplate.opsForList();
// 存值
listOperations.leftPush("mylist","a");
listOperations.leftPushAll("mylist","b","c","d");
// 取值
List<String> mylist = listOperations.range("mylist", 0, -1);
for (String value : mylist) {
System.out.println(value);
}
// 获得列表长度 llen
Long size = listOperations.size("mylist");
int lSize = size.intValue();
for (int i = 0; i < lSize; i++) {
// 出队列
String element = (String) listOperations.rightPop("mylist");
}
}
/**
* 操作Set类型数据
*/
@Test
public void testSet(){
SetOperations setOperations = redisTemplate.opsForSet();
// 存值
setOperations.add("myset","a","b","c","a");
// 取值
Set<String> myset = setOperations.members("myset");
for (String s : myset) {
System.out.println(s);
}
// 删除成员
setOperations.remove("myset","a","b");
// 取值
myset = setOperations.members("myset");
for (String s : myset) {
System.out.println(s);
}
}
/**
* 操作ZSet类型数据
*/
@Test
public void testZSet(){
ZSetOperations zSetOperations = redisTemplate.opsForZSet();
// 存值
zSetOperations.add("myZset","a",10.0);
zSetOperations.add("myZset","b",11.0);
zSetOperations.add("myZset","c",12.0);
zSetOperations.add("myZset","d",13.0);
// 取值
Set<String> myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
// 修改分数
zSetOperations.incrementScore("myZset","b",20.0);
// 删除成员
zSetOperations.remove("myZset","a","b");
// 取值
myZset = zSetOperations.range("myZset", 0, -1);
for (String s : myZset) {
System.out.println(s);
}
}
/**
* 通用操作,针对不同的数据类型都可以操作
*/
@Test
public void testCommon(){
// 获取Redis中所有的key
Set<String> keys = redisTemplate.keys("*");
for (String key : keys) {
System.out.println(key);
}
// 判断某个key是否存在
Boolean shryu = redisTemplate.hasKey("shryu");
System.out.println(shryu);
// 删除指定key
redisTemplate.delete("myZset");
// 获取指定key对应的value的数据类型
DataType dataType = redisTemplate.type("myset");
System.out.println(dataType.name());
}
}