常用操作

  1. public class RedisTest{
  2. @Resource
  3. private StringRedisTemplate stringRedisTemplate;
  4. public void testRedisList(){
  5. List<String> skuIds = new ArrayList<>();
  6. // 往redis中存入整个List<String>
  7. stringRedisTemplate.opsForList().leftPushAll("gulimall:seckill:sessions:12345", skuIds);
  8. // 获取redis中所有指定格式的key, * 代表不确定的字符
  9. Set<String> keys = stringRedisTemplate.keys("gulimall:seckill:sessions:" + "*");
  10. // 取出redis的list中索引从-100到100的所有值
  11. List<String> skuIds = stringRedisTemplate.opsForList().range("gulimall:seckill:sessions:12345", -100, 100);
  12. }
  13. public void testRedisHash(){
  14. // 绑定Redis的Hash,准备Hash操作
  15. BoundHashOperations<String, String, String> skusOperations = stringRedisTemplate
  16. .boundHashOps("gulimall:seckill:skus");
  17. // 存入数据,k、v都得是String类型
  18. skusOperations.put(relation.getSkuId().toString(), jsonStr);
  19. // 从Hash中批量获取多个key的值
  20. List<String> values = skusOperations.multiGet(skuIds);
  21. }
  22. public void testRedisAbsent(){
  23. // 尝试在Redis中占位
  24. Boolean absent = stringRedisTemplate.opsForValue().setIfAbsent("123_456_789", "1000", 145632549L,
  25. TimeUnit.MILLISECONDS);
  26. }
  27. }

存储获取缓存值

RedisKeyUtil.java

  1. /*
  2. * 首页猜你喜欢商品集合
  3. *
  4. * @param regionCode
  5. * @return
  6. */
  7. public static String getGuessYouLikeProductsKey(Integer regionCode) {
  8. return MessageFormat.format("{0}:home:like:product:{1}", PROJECT_SPACE, String.valueOf(regionCode));
  9. }

RetailHomeServiceImpl.java

  1. @Resource
  2. private RedisService redisService;
  3. private List<Long> getProductIds(List<Long> mchIds, String key) {
  4. String key = RedisKeyUtil.getGuessYouLikeProductsKey(regionCode);
  5. String jsonString = redisService.get(key);
  6. List<Long> productIds;
  7. if (StringUtils.isNotEmpty(jsonString)) {
  8. // 缓存不为空,直接取缓存
  9. productIds = JSON.parseObject(jsonString, new TypeReference<List<Long>>() {
  10. });
  11. } else {
  12. // 缓存为空,则重新查询
  13. productIds = retailProductService.getRandomProductIds(mchIds);
  14. // 要缓存的值
  15. String redisValue = JSON.toJSONString(productIds);
  16. // 存入redis
  17. redisService.set(key, redisValue, RetailPortalConstant.ORDER_TIMEOUT_PAY, TimeUnit.MINUTES);
  18. }
  19. return productIds;
  20. }

分布式锁

配置类

  1. @Configuration
  2. public class RedissonConfig {
  3. /**
  4. * 所有对Redisson的使用都是通过RedissonClient对象
  5. * @return
  6. */
  7. @Bean(destroyMethod = "shutdown") // 服务停止以后,会调用shutdown进行销毁
  8. RedissonClient redission() {
  9. Config config = new Config();
  10. config.useSingleServer().setAddress("redis://192.168.2.190:6379");
  11. return Redisson.create(config);
  12. }
  13. }

使用

  1. public class RedissonTest{
  2. @Resource
  3. private RedissonClient redissonClient;
  4. public void test1(){
  5. // 为了限流:使用秒杀商品的库存作为分布式的信号量
  6. RSemaphore semaphore = redissonClient.getSemaphore("gulimall:seckill:semaphore:11984846");
  7. // 为信号量设置值
  8. semaphore.trySetPermits(relation.getSeckillCount());
  9. // 让信号量尝试扣减值
  10. try {
  11. // 要扣的数量,等待的时间,时间单位
  12. boolean acquire = semaphore.tryAcquire(1, 100, TimeUnit.MILLISECONDS);
  13. if (acquire) {
  14. // 扣减成功
  15. }
  16. } catch (InterruptedException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }