一个使用C语言编写的key-value的数据库,也被称作NoSQL数据库。用于高速缓存数据,存放在内存中。
string
list
set
sorted set
hash
其中的Jedis类用于访问Redis服务器,可以与Server建立连接并发送命令
// 建立Redis的操作对象
Jedis jedis = new Jedis("192.168.12.39","6379");
//密码验证
String status = jedis.auth("123123");
// 消息验证
sout("ping=" + jedis.ping());
// 设置key-value
jedis.set("jedis-key1","value");
// 从缓存获取值
String val = jedis.get("jedis-key1");
sout(val);
// 关闭连接
try{
}finally{
if(jedis != null){
jedis.close();
}
}
发布订阅(pub/sub)是一种消息通信模式:发布者(pub)发送消息,订阅 者(sub)接收消息。
redis作为一个server,在订阅者和发布者之间起到了消息路由的功能, 解耦消息发布者和消息订阅者。
订阅者可以通过subscribe和psubscribe命令向redis server订阅自己感兴趣的消息,发布者通过publish命令向redis server发送特定类型的消 息。
• 一个client可以订阅多个 channel,一个channel也可以被多个client订阅。
• 当客户端通过subscribe或psubscribe订阅后就进入订阅模式,除非使
用unsubscribe或punsubscribe命令退出订阅模式,否则不能发送其他 命令。
• 监听
MyPubSub ps = new MyPubSub();
jedis.subscribe(ps, “channel1");
注:MyPubSub类继承自JedisPubSub类, 它是Redis驱动包提供的一 个抽象类,用于实现对消息的处理
JedisPubSub类中提供的可被子类重写的方法
public class MyPubSub extends JedisPubSub {
//从订阅的频道收到消息时的回调
public void onMessage(String channel, String message) { }
//从订阅的频道模式收到消息时的回调
public void onPMessage(String pattern, String channel, String message) { }
//订阅频道时的回调
public void onSubscribe(String channel, int subscribedChannels) { }
//取消订阅频道时的回调
public void onUnsubscribe(String channel, int subscribedChannels) {}
public void onPUnsubscribe(String pattern, int subscribedChannels) {}
public void onPSubscribe(String pattern, int subscribedChannels) {}
}
Spring Data Redis(SDR)是Spring提供的组件,用来简化访问Redis,它也是对Jedis组件的封装。
spring boot使用lettuce作为客户端jar,它是线程安全的
在application.properties配置:
server.port=8081
server.servlet.context-path=/spbSdr
#Redis配置
spring.redis.host=192.168.12.39
spring.redis.port=6379
spring.redis.password=123123
spring.redis.database=1
spring.redis.timeout=5000
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0
在RedisConfig配置:
package com.yq.spbsdr01;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
import java.time.Duration;
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Resource
private LettuceConnectionFactory lettuceConnectionFactory;
public CacheManager cacheManager() {
// 生成默认配置config对象
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// 设置缓存的默认过期时间,并且不缓存空值
config = config.entryTtl(Duration.ofMinutes(10)).disableCachingNullValues();
// 设置针对key和value所使用的序列化器
config = config.serializeKeysWith(
RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
return RedisCacheManager.builder(lettuceConnectionFactory).cacheDefaults(config).build();
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
// 设置针对key和value所使用的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
}