缓存

it2024-12-03  2

Redis 的数据结构 String List Set Sorted Set Hash Geo Stream

Redis 实现分布式锁: 学习链接

Redis 发布-订阅的实现 (可以订阅指定的key,或者订阅某种类型事件,如del expired) a. java 代码的方式: 发布者代码:

@Service public class RedisPublishDemo1 { @Autowired private RedisTemplate redisTemplate; public final static String SMS_NAME = "smsName"; public void publish(){ redisTemplate.execute(new RedisCallback() { @Override public Object doInRedis(RedisConnection redisConnection) throws DataAccessException { redisConnection.publish(SMS_NAME.getBytes(),"来自加菲猫的短信".getBytes()); return null; } }); } }

订阅者代码:

@Component public class SmsChannelListener { @Autowired private RedisTemplate redisTemplate; @PostConstruct public void subscribe(){ redisTemplate.execute(new RedisCallback() { @Override public Object doInRedis(RedisConnection redisConnection) throws DataAccessException { redisConnection.subscribe(new MessageListener() { @Override public void onMessage(Message message, byte[] bytes) { System.out.println("收到消息:" + message); } },RedisPublishDemo1.SMS_NAME.getBytes()); return null; } }); } }

测试类:

@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class RedisPublishDemo1Test { @Autowired private RedisPublishDemo1 redisPublishDemo1; //测试发布/订阅 @Test public void testPubSub(){ redisPublishDemo1.publish(); } }

测试结果:

b.Spring 的方式实现订阅者:

@Component public class SmsChannelListener2 { @Bean public RedisMessageListenerContainer subscribeRedisMessageListenerContainer(RedisConnectionFactory factory){ RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(factory); container.addMessageListener((message, bytes) -> { System.out.println("用Spring 的方式实现,收到messgae:"+message); }, Arrays.asList(new ChannelTopic(RedisPublishDemo1.SMS_NAME))); return container; } } Redis过期事件的监听 @Configuration public class RedisDelayedQueueDemo1 { @Autowired private RedisTemplate redisTemplate; private final String DELAYED_QUEME_MESSAGE_NAME = "delayedQueueMessage"; @Bean public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory){ RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(factory); //监听过期key container.addMessageListener(new MyMessageListener(),new PatternTopic("__keyevent@0__:expired")); return container; } } class MyMessageListener implements MessageListener{ @Override public void onMessage(Message message, byte[] bytes) { //todo 业务代码实现 } } Redis 的 increment()必须是stringRedisTemplate模板才能使用 @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void test4(){ //increment方法必须是stringRedisTemplate模板才能使用 Long count1 = stringRedisTemplate.opsForValue().increment("count1", 1); System.out.println(count1.longValue()); } Redis 实现延迟队列 import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.data.redis.connection.Message; import org.springframework.data.redis.connection.MessageListener; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.listener.PatternTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID; import java.util.concurrent.TimeUnit; @Configuration @Profile("delayed-queue") public class RedisDelayedQueueDemo1 { private final static String KEY_NAME = "keyName"; private final static String CONTENT_NAME = "contentName"; @Autowired private RedisTemplate redisTemplate; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Bean public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory){ RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(factory); container.addMessageListener(new DelayedQueueMessageListener(),new PatternTopic("__keyevent@0__:expired")); return container; } public void create(){ String orderId = UUID.randomUUID().toString(); redisTemplate.opsForValue().set(orderId,KEY_NAME,3, TimeUnit.SECONDS); String content = "{\"count\":1,\"ttl\":3,\"content\":\"加菲猫\"}"; redisTemplate.opsForValue().set(CONTENT_NAME + orderId ,content); } class DelayedQueueMessageListener implements MessageListener { @Override public void onMessage(Message message, byte[] bytes) { String content = (String) redisTemplate.opsForValue().get(CONTENT_NAME + message); JSONObject jsonObject = JSONObject.parseObject(content); Integer count = jsonObject.getInteger("count"); Integer ttl = jsonObject.getInteger("ttl"); String content1 = jsonObject.getString("content"); if(count <= 5){ System.out.println("消息为:" + content1 + ";;count :" + count); System.out.println("当前时间:" + simpleDateFormat.format(new Date())); //修改重试次数和过期时间 count++; ttl = ttl * 2; jsonObject.put("count",count); jsonObject.put("ttl",ttl); redisTemplate.opsForValue().set(CONTENT_NAME + message,jsonObject.toJSONString()); redisTemplate.opsForValue().set(message.toString(),KEY_NAME,ttl,TimeUnit.SECONDS); }else{ redisTemplate.delete(CONTENT_NAME + message); System.out.println("删除********"); } } } }
最新回复(0)