所有分类
  • 所有分类
  • 未分类

Redisson-使用/教程/实例

简介

说明

本文用示例介绍Redisson的用法。

官网

官网:https://redisson.org/
git:https://github.com/redisson/redisson
git文档:https://github.com/redisson/redisson/wiki

常用操作 

获得Redisson客户端

@Autowired
private RedissonClient redissonClient;

根据key获得value

所有get开头的方法:如果key不存在会在保存时以此key为key保存。

getList。它返回RList,RList实现了java.util.List。所以我们可以像操作List一样操作RList。(其他类型,例如:RMap等也是一样,和JDK的接口一样的操作)。

将key-value写到Redis

所有添加都会自动将值写到Redis。例如:

RSet<String> set = redisson.getSet("anySet");
set.add("Tony");

示例:任意对象(包括String)

package com.knife.test;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/test")
    public String test(){
        RBucket<Object> bucket = redissonClient.getBucket("key1");
        // 此处可以是任意对象,不仅仅是字符串
        bucket.set("value1");
        
        String str1 = (String) rBucket.get();
        System.out.println(str1);// 此处会打印出:value1

        // 删除"key1"这一项数据
        bucket.delete();
        
        return "success";
    }
}

示例:集合类型

本处只展示getList。它返回RList,RList实现了java.util.List。所以我们可以像操作List一样操作RList。(其他类型,例如:RMap等也是一样,和JDK的接口一样的操作)

package com.knife.test;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/test")
    public String test(){
        RList<Object> list1 = redissonClient.getList("list1");
        list1.add(123);
        list1.add("abc");

        list1.remove("abc");
        
        return "success";
    }
}

示例:分布式锁

package com.knife.order;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 提交订单
     */
    @PostMapping("/submitOrder")
    public String submitOrder(){
        RLock lock = redissonClient.getLock(key);
        // 这里lock不可能为null。
        // 官方文档就是这么写的,你要实在想判断null也可以。

        boolean locked = false;
        locked = lock.tryLock();
        if (!locked) {
            throw new RuntimeException("正在处理中,请稍后重试");
        }

        try {
            // 处理逻辑
        } finally {
            lock.unlock();
        }
        

        // 写法2:阻塞写法
        // try {
        //     // 阻塞,直到获取到锁
        //     lock.lock();
        //     
        //     //数据库操作:插入订单数据,扣减库存等
        // } finally {
        //     lock.unlock();//释放锁
        // }
        
        
        // 写法3:带超时时间
        // try {
        //     // 获取到锁则locked为true,超过三秒没获取到锁则locked为false
        //     locked = lock.tryLock(3, TimeUnit.SECONDS);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }
        // if (!locked) {
        //     throw new RuntimeException("正在处理中,请稍后重试");
        // }
        // try {
        //     // 处理逻辑
        // } finally {
        //     lock.unlock();
        // }

        return "success";
    }
}

示例:模糊查询

官网网址

RKeys – redisson 3.10.0 javadoc

相关网址

Redis–模糊查询–方法/实例_IT利刃出鞘的博客-CSDN博客

实例

package com.knife.test;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private RedissonClient redissonClient;

    
    @GetMapping("/test")
    public String test(){
        RKeys keys = redissonClient.getKeys();
        
        Iterable<String> keysByPattern = keys.getKeysByPattern("abc*");

        long delete = keys.deleteByPattern("def?");
        
        return "success";
    }
}

示例:过期时间

所有的类型都有过期时间,例如:

RSet<String> set = redisson.getSet("anySet");
set.add("Tony");
set.expire(3, TimeUnit.MINUTES);

过期时间在类型为空时是无效的,例如:

RSet<String> set = redisson.getSet("anySet");
set.expire(3, TimeUnit.MINUTES);
set.add("Tony");

给Set的某一个值设置过期时间

RSetCache<String> set = redisson.getSetCache("anySet");
// ttl = 10 seconds
set.add("abc", 10, TimeUnit.SECONDS);

给Map的某一个小Key设置过期时间

RMapCache<Object, Object> outerMap = redissonClient.getMapCache("outer");
outerMap.put("innerKey1", "innerValue1", 10, TimeUnit.SECONDS);

数据类型

方法返回类型
getSet(“xxx”)RSet<SomeObject>
getList(“xxx”)RList<SomeObject>
getMap(“anyMap”);RMap<String, SomeObject>
getSetMultimap(“myMultimap”);RSetMultimap<SimpleKey, SimpleValue>
getSortedSet(“anySet”);RSortedSet<Integer>
getScoredSortedSet(“simple”);RScoredSortedSet<SomeObject>
getLexSortedSet(“simple”);RLexSortedSet set
getQueue(“anyQueue”);RQueue<SomeObject>
getDeque(“anyDeque”);RDeque<SomeObject>
getBlockingQueue(“anyQueue”);BlockingQueue<SomeObject>
getBoundedBlockingQueue(“anyQueue”);RBoundedBlockingQueue<SomeObject>
getBlockingDeque(“anyDeque”);RBlockingDeque<Integer>
getBlockingFairQueue(“myQueue”);RBlockingFairQueue
getBlockingFairDeque(“myDeque”);RBlockingFairDeque
getDelayedQueue(distinationQueue);RDelayedQueue<String
getPriorityQueue(“anyQueue”);RPriorityQueue<Integer>
getPriorityDeque(“anyQueue”);RPriorityDeque<Integer>
getPriorityBlockingQueue(“anyQueue”);RPriorityBlockingQueue<Integer>
RPriorityBlockingDeque<Integer>getPriorityBlockingDeque(“anyQueue”);
0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录