下载镜像
docker pull redis:alpine3.17
查看
docker images
创建容器
docker run \
--restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6379:6379 \
--name redis \
-v /home/redis/myredis/myredis.conf:/etc/redis/redis.conf \
-v /home/redis/myredis/data:/data \
-d redis:alpine3.17 redis-server /etc/redis/redis.conf \
--appendonly yes \
--requirepass 21yy1314
--restart=always 启动方式,开机自启动
--log-opt 日志配置
-p 6379:6379 端口映射
--name redis 容器名称
-v 挂在卷地址,主机地址,容器地址。以后修改本机中文件,自动同步到容器中
-d redis启动使用/etc/redis/redis.conf配置文件
--appendonly yes 开启持久化
--requirepass 设置密码
创建成功
1.redis的字符类型
2.redis常见命令
help @generic //查看通用命令 generic可更换为具体的组
2.1.通用命令
2.2 String类型常见命令
String 有三种数据结构 : Sting, int, float 其中储存编码方式不同
2.3 key 的结构
2.4 hash 类型
2.4 List类型
2.5 Set类型
2.6 sortedset类型
3.redis 的客户端
1.jedis
import com.yyxxlu.util.JedisConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import java.util.Map;
/**
* @author YY
* @date 2024/05/08/17:48
*/
public class JedisTest {
private Jedis jedis;
@BeforeEach
void setUp() {
// 建立连接
// jedis=new Jedis("192.168.150.20",6379);
// 使用连接池
jedis=JedisConnectionFactory.getJedis();
// 设置密码
jedis.auth("21yy1314");
// 选择库
jedis.select(0);
}
// 测试字符串类型
@Test
void testString() {
// 存入数据
String result = jedis.set("name", "胡歌");
System.out.println(result);
// 取到数据
String name = jedis.get("name");
System.out.println("name: "+name);
}
// 测试哈希类型
@Test
void testHash() {
jedis.hset("user:1","name","jack");
jedis.hset("user:1","age","21");
Map<String, String> map = jedis.hgetAll("user:1");
System.out.println(map);
}
// 关闭连接
@AfterEach
void tearDown() {
if (jedis != null) {
jedis.close();
}
}
}
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.time.Duration;
/**
* @author YY
* @date 2024/05/08/18:15
*/
public class JedisConnectionFactory {
private static final JedisPool jedispool;
static {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 设置最大空闲连接
jedisPoolConfig.setMaxIdle(8);
// 设置最大连接
jedisPoolConfig.setMaxTotal(8);
// 设置最小空闲连接
jedisPoolConfig.setMinIdle(0);
// 设置最长等待时间
jedisPoolConfig.setMaxWait(Duration.ofSeconds(2));
jedispool= new JedisPool(jedisPoolConfig,"192.168.150.20",6379,1000,"21yy1314");
}
public static Jedis getJedis() {
return jedispool.getResource();
}
}
2.SpringDataRedis
现在可使用redis
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.core.RedisTemplate;
@SpringBootTest
class SpringDataRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
// 测试string
@Test
void contextLoads() {
redisTemplate.opsForValue().set("name","杨哥");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}
但是发现SpringDataredis的默认序列化为JDK的,会导致数据混淆,不太好用
重新定义序列化方法
package com.yyxxlu.springdataredis.Redis.Config;
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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* @author YY
* @date 2024/05/10/17:48
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
// 创建RedisTemplate对象
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
// 设置连接工厂
redisTemplate.setConnectionFactory(connectionFactory);
// 创建JSON序列化工具
GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer =new GenericJackson2JsonRedisSerializer();
// 设置默认的key的序列化
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
// 设置value的序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
// 返回
return redisTemplate;
}
}
package com.yyxxlu.springdataredis.Redis.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author YY
* @date 2024/05/10/18:03
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private Integer age;
}
package com.yyxxlu.springdataredis;
import com.yyxxlu.springdataredis.Redis.pojo.User;
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.core.RedisTemplate;
@SpringBootTest
class SpringDataRedisApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
void SaveUser() {
redisTemplate.opsForValue().set("user:1",new User("虎哥",32));
User user = (User) redisTemplate.opsForValue().get("user:1");
System.out.println(user);
}
// 测试string
@Test
void contextLoads() {
redisTemplate.opsForValue().set("name","杨哥");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}
但是这样自动装配会导致redis内存占用多一部分
不太好,所以
package com.yyxxlu.springdataredis;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yyxxlu.springdataredis.Redis.pojo.User;
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.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@SpringBootTest
class SpringDataRedisTests {
@Autowired
private StringRedisTemplate redisTemplate;
// 序列化工具
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void SaveUser() throws JsonProcessingException {
// 创建对象
User user =new User("虎哥",32);
// 手动序列化
String s = mapper.writeValueAsString(user);
mapper.writeValueAsString(user);
redisTemplate.opsForValue().set("user:10",s);
String tt = redisTemplate.opsForValue().get("user:10");
// 手动反序列化
User user1=mapper.readValue(s,User.class);
System.out.println(user1);
}
// 测试string
@Test
void contextLoads() {
redisTemplate.opsForValue().set("name","和技术哥");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}