spring boot(三)整合 redis

Spring boot 集成redis

为什么要用redis,它解决了什么问题?

Redis 是一个高性能的key-value内存数据库。它支持常用的5种数据结构:String字符串、Hash哈希表、List列表、Set集合、Zset有序集合 等数据类型。
Redis它解决了2个问题:
第一个是:性能
通常数据库的读操作,一般都要几十毫秒,而redisd的读操作一般仅需不到1毫秒。通常只要把数据库的数据缓存进redis,就能得到几十倍甚至上百倍的性能提升。
第二个是:并发
在大并发的情况下,所有的请求直接访问数据库,数据库会出现连接异常,甚至卡死在数据库中。为了解决大并发卡死的问题,一般的做法是采用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问数据库。

一、添加maven依赖

        
            org.springframework.boot
            spring-boot-starter-data-redis
        

二、配置文件

#端口
server.port=9998


## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=



### Redis集群版配置
### 连接池最大连接数(使用负值表示没有限制)
#spring.redis.lettuce.pool.max-active=8
### 连接池中的最大空闲连接
#spring.redis.lettuce.pool.max-idle=8
### 连接池最大阻塞等待时间(使用负值表示没有限制)
#spring.redis.lettuce.pool.max-wait=-1ms
### 连接池中的最小空闲连接
#spring.redis.lettuce.pool.min-idle=0
##spring.redis.sentinel.master= # Name of Redis server.
##spring.redis.sentinel.nodes= # Comma-separated list of host:port pairs.
#spring.redis.timeout=1m
#spring.redis.cluster.nodes=81.70.199.73:6381,81.70.199.73:6382

三、设置redis配置,key和value序列化的

@Slf4j
@Configuration
public class RedisConfiguration {
    /**
     * 重写Redis序列化方式,使用Json方式:
     * 当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer,StringRedisTemplate默认使用的是StringRedisSerializer。
     * Spring Data JPA为我们提供了下面的Serializer:
     * GenericToStringSerializer、Jackson2JsonRedisSerializer、JacksonJsonRedisSerializer、
     * JdkSerializationRedisSerializer、OxmSerializer、StringRedisSerializer。
     *
     * 在此我们将自己配置RedisTemplate并定义Serializer。
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        log.info("设置redisTemplate序列化 getKeySerializer:{} getValueSerializer:{}",redisTemplate.getKeySerializer().getClass(),redisTemplate.getValueSerializer().getClass());
        log.info("设置redisTemplate序列化 getHashKeySerializer:{} getHashValueSerializer:{}",redisTemplate.getHashKeySerializer().getClass(),redisTemplate.getHashValueSerializer().getClass());
        return redisTemplate;
    }
}

四、使用RedisTemplate

string

@Slf4j
@Api("redis【string】")
@RestController
@RequestMapping("/redis/string")
public class RedisStringController {

    @Autowired
    RedisTemplate redisTemplate;
    //-----------------redis string start-------------
    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp string_initData(){
        log.info("string_initData start");
        for (User user : TestDataUtil.getUsers()) {
            log.info("string_initData user:{}",user);
            redisTemplate.opsForValue().set(user.getId().toString(),user);
        }
        log.info("string_initData end");
        return BaseResp.buildSuccess();
    }

    @ApiOperation("get方法")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public BaseResp string_get(@RequestParam("strKey")String strKey){
        Object val = redisTemplate.opsForValue().get(strKey);
        return BaseResp.buildDataSuccess(val);
    }

    @ApiOperation("set方法")
    @RequestMapping(value = "/string/set",method = RequestMethod.GET)
    public BaseResp string_set(@RequestParam("strKey")String strKey,@RequestParam("strVal")String strVal){
        redisTemplate.opsForValue().set(strKey,strVal);
        return BaseResp.buildSuccess();
    }
    //-----------------redis string end-------------

}

set

@Slf4j
@Api("redis【set】")
@RestController
@RequestMapping("/redis/set")
public class RedisSetController {

    @Autowired
    RedisTemplate redisTemplate;



    //-----------------redis set start-------------
    String testSetKey = "test_set";
    String testSetKey_1 = "test_set_1";
    String testSetKey_2 = "test_set_2";
    String testSetKey_3 = "test_set_3";

    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp set_initData(){
        log.info("list_initData start");
        Long resp = redisTemplate.opsForSet().add(testSetKey,TestDataUtil.getUsers().toArray());

        redisTemplate.opsForSet().add(testSetKey_1,"A","B","C");
        redisTemplate.opsForSet().add(testSetKey_2,"A","a","B");
        redisTemplate.opsForSet().add(testSetKey_3,"A");
        log.info("list_initData end resp:{}",resp);
        return BaseResp.buildSuccess();
    }

    @ApiOperation("set-拉取")
    @RequestMapping(value = "/pop",method = RequestMethod.GET)
    public BaseResp set_pop(){
        SetOperations setOperations  = redisTemplate.opsForSet();
        User user= setOperations.pop(testSetKey);
        return BaseResp.buildDataSuccess(user);
    }

    @ApiOperation("randomMember 随机返回一个元素")
    @RequestMapping(value = "/randomMember",method = RequestMethod.GET)
    public BaseResp set_randomMember(){
        SetOperations setOperations  = redisTemplate.opsForSet();
        User user= setOperations.randomMember(testSetKey);
        return BaseResp.buildDataSuccess(user);
    }

    @ApiOperation("添加一个元素")
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public BaseResp set_add(@RequestParam("setKey")String setKey,@RequestParam("setVal")String setVal){
        SetOperations setOperations  = redisTemplate.opsForSet();
        Long resp = setOperations.add(setKey,setVal);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation("判断一个元素是否存在")
    @RequestMapping(value = "/isMember",method = RequestMethod.GET)
    public BaseResp set_isMember(@RequestParam("setKey")String setKey,@RequestParam("setVal")String setVal){
        SetOperations setOperations  = redisTemplate.opsForSet();
        boolean resp = setOperations.isMember(setKey,setVal);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation("获取set的元素数量")
    @RequestMapping(value = "/size",method = RequestMethod.GET)
    public BaseResp list_pop1(@RequestParam("setKey")String setKey){
        SetOperations setOperations  = redisTemplate.opsForSet();
        return BaseResp.buildDataSuccess(setOperations.size(setKey));
    }



    @ApiOperation("多个set-差集(取第一个set集合中 在其他set集合没有出现的元素)")
    @RequestMapping(value = "/difference",method = RequestMethod.GET)
    public BaseResp list_difference(){
        SetOperations setOperations  = redisTemplate.opsForSet();
        Set differenceSet = setOperations.difference(Arrays.asList(testSetKey_1,testSetKey_3,testSetKey_2));
        return BaseResp.buildDataSuccess(differenceSet);
    }

    @ApiOperation("多个set-并集")
    @RequestMapping(value = "/union",method = RequestMethod.GET)
    public BaseResp list_union(){
        SetOperations setOperations  = redisTemplate.opsForSet();

        Set differenceSet = setOperations.union(Arrays.asList(testSetKey_1,testSetKey_2,testSetKey_3));
        return BaseResp.buildDataSuccess(differenceSet);
    }
    @ApiOperation("多个set-交集")
    @RequestMapping(value = "/intersect",method = RequestMethod.GET)
    public BaseResp list_intersect(){
        SetOperations setOperations  = redisTemplate.opsForSet();
        Set differenceSet = setOperations.intersect(Arrays.asList(testSetKey_1,testSetKey_2,testSetKey_3));
        return BaseResp.buildDataSuccess(differenceSet);
    }

    //-----------------redis set end-------------

}

hash

@Slf4j
@Api("redis【hash】")
@RestController
@RequestMapping("/redis/hash")
public class RedisHashController {

    @Autowired
    RedisTemplate redisTemplate;

    String testHashKey = "hash_{}";

    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp hash_initData(){
        log.info("hash_initData start");
        for (User user : TestDataUtil.getUsers()) {
            log.info("hash_initData user:{}",user);
            redisTemplate.opsForHash().putAll(StrUtil.format(testHashKey,user.getId()),JSONUtil.parseObj(user));
        }
        log.info("hash_initData end");
        return BaseResp.buildSuccess();
    }
    @ApiOperation("put方法")
    @RequestMapping(value = "/put",method = RequestMethod.GET)
    public BaseResp hash_put(@RequestParam("h")String h,@RequestParam("hk")String hk,@RequestParam("hv")String hv){
        redisTemplate.opsForHash().put(h,hk,hv);
        return BaseResp.buildSuccess();
    }
    @ApiOperation("get方法")
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public BaseResp hash_get(@RequestParam("h")String h,@RequestParam("hk")String hk){
        redisTemplate.opsForHash().get(h,hk);
        return BaseResp.buildSuccess();
    }
    @ApiOperation("increment方法")
    @RequestMapping(value = "/increment",method = RequestMethod.GET)
    public BaseResp hash_increment(@RequestParam("h")String h,@RequestParam("hk")String hk){
        redisTemplate.opsForHash().increment(h,hk,1);
        return BaseResp.buildSuccess();
    }
    @ApiOperation("keys方法")
    @RequestMapping(value = "/keys",method = RequestMethod.GET)
    public BaseResp hash_keys(@RequestParam("h")String h){
        return BaseResp.buildDataSuccess(redisTemplate.opsForHash().keys(h));
    }

    @ApiOperation("keys方法")
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public BaseResp delete(@RequestParam("h")String h){
        HashOperations hashOperations = redisTemplate.opsForHash();
        Long resp = hashOperations.delete(h);
        return BaseResp.buildDataSuccess(resp);
    }
}

list


@Slf4j
@Api("redis【list】")
@RestController
@RequestMapping("/redis/list")
public class RedisListController {

    @Autowired
    RedisTemplate redisTemplate;

    //-----------------redis list start-------------
    String testListKey = "test_list";

    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp list_initData(){
        log.info("list_initData start");
        redisTemplate.opsForList().rightPushAll(testListKey,TestDataUtil.getUsers().toArray());
        log.info("list_initData end");
        return BaseResp.buildSuccess();
    }

    @ApiOperation("根据下标 取数据")
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public BaseResp list_index(@RequestParam("index")Integer index){
        Object user = redisTemplate.opsForList().index(testListKey,index);
        return BaseResp.buildDataSuccess(user);
    }

    @ApiOperation("根据下标 取数据")
    @RequestMapping(value = "/list/range",method = RequestMethod.GET)
    public BaseResp range(@RequestParam("start")Integer start,@RequestParam("end")Integer end){
        Object user = redisTemplate.opsForList().range(testListKey,start,end);
        return BaseResp.buildDataSuccess(user);
    }
    String testListPopKey = "test_list_pop";

    @ApiOperation("list-拉取")
    @RequestMapping(value = "/pop",method = RequestMethod.GET)
    public BaseResp list_pop(){
        Object popResp = redisTemplate.opsForList().leftPop(testListPopKey, Duration.ofMinutes(1));
        log.info("popResp:{}",popResp);
        return BaseResp.buildDataSuccess(popResp);
    }
    @ApiOperation("list-推送")
    @RequestMapping(value = "/pop/leftPush",method = RequestMethod.GET)
    public BaseResp list_pop(@RequestParam("pushVal")String pushVal){
        Long resp = redisTemplate.opsForList().leftPush(testListPopKey,pushVal);
        return BaseResp.buildDataSuccess(resp);
    }
    //-----------------redis list end-------------
}

zset


@Slf4j
@Api("redis【zSet】")
@RestController
@RequestMapping("/redis/zSet")
public class RedisZSetController {

    @Autowired
    RedisTemplate redisTemplate;

    //-----------------redis zset start-------------
    String zSet_test_key = "zSet_test";

    String zSet_test_key_1 = "zSet_test_1";


    @ApiOperation("初始化数据")
    @RequestMapping(value = "/initData",method = RequestMethod.GET)
    public BaseResp zSet_initData(){
        log.info("zSet_initData start");
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        List users = TestDataUtil.getUsers();
        for (User user : users) {
            setOperations.add(zSet_test_key,user,user.getAge());
        }
        log.info("zSet_initData end");
        return BaseResp.buildSuccess();
    }

    @ApiOperation("【zSet】初始化数据(添加 类型是TypedTuple的set集合)")
    @RequestMapping(value = "/initData_tuples",method = RequestMethod.GET)
    public BaseResp zSet_initData1(){
        log.info("zSet_initData1 start");
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        List users = TestDataUtil.getUsers();

        Set> tuples = new HashSet<>();
        for (User user : users) {
            tuples.add(ZSetOperations.TypedTuple.of(user,user.getAge().doubleValue()));
        }
        Long resp = setOperations.add(zSet_test_key, tuples);
        log.info("zSet_initData1 end resp:{}",resp);
        return BaseResp.buildSuccess();
    }

    @ApiOperation("【zSet】初始化数据 (value值只存id)")
    @RequestMapping(value = "/initData_value",method = RequestMethod.GET)
    public BaseResp initData_value(){
        log.info("initData_value start");
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        List users = TestDataUtil.getUsers();
        for (User user : users) {
            setOperations.add(zSet_test_key_1,user.getId(),user.getAge());
        }
        log.info("initData_value end");
        return BaseResp.buildSuccess();
    }

    @ApiOperation("计算有序集合中指定字典区间(minScore -> maxScore)内成员数量。")
    @RequestMapping(value = "/count",method = RequestMethod.GET)
    public BaseResp zSet_count(@RequestParam("minScore")Double minScore,@RequestParam("maxScore")Double maxScore){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Long count =setOperations.count(zSet_test_key_1,minScore,maxScore);
        return BaseResp.buildDataSuccess(count);
    }

    @ApiOperation("有序集合中,元素score原子自增")
    @RequestMapping(value = "/incrementScore",method = RequestMethod.GET)
    public BaseResp incrementScore(@RequestParam("uid")Integer uid,@RequestParam(value = "score",defaultValue = "1")Integer score){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Double count = setOperations.incrementScore(zSet_test_key_1,uid,score);
        return BaseResp.buildDataSuccess(count);
    }
    @ApiOperation("取有序集合中 成员数量")
    @RequestMapping(value = "/count/zCard",method = RequestMethod.GET)
    public BaseResp zCard(){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Long count = setOperations.zCard(zSet_test_key_1);
        return BaseResp.buildDataSuccess(count);
    }

    @ApiOperation("取当前成员所在 集合的排名【升序的排名(按照score从低到高的 排名)】【排名是所在下标 从0开始】")
    @RequestMapping(value = "/get/rank",method = RequestMethod.GET)
    public BaseResp rank(@RequestParam("uid")Integer uid){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Long rank = setOperations.rank(zSet_test_key_1,uid);
        log.info("rank uid:{} rank:{}",uid,rank);
        return BaseResp.buildDataSuccess("所在排名"+(rank+1));
    }


    @ApiOperation("取当前成员所在 集合的排名【降序的排名(按照score从高到低的 排名)】【排名是所在下标 从0开始】")
    @RequestMapping(value = "/get/rank/reverse",method = RequestMethod.GET)
    public BaseResp reverseRank(@RequestParam("uid")Integer uid){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Long reverseRank = setOperations.reverseRank(zSet_test_key_1,uid);
        log.info("rank uid:{} reverseRank:{}",uid,reverseRank);
        return BaseResp.buildDataSuccess("所在排名"+(reverseRank+1));
    }



    @ApiOperation("根据设置的最大和最小的范围,返回字典排序集合中的元素")
    @RequestMapping(value = "/get/rangeByLex",method = RequestMethod.GET)
    public BaseResp rangeByLex(@RequestParam(value = "minScore",required = true,defaultValue = "0")Double minScore,
                               @RequestParam(value = "maxScore",required = true,defaultValue = "999")Double maxScore){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        RedisZSetCommands.Range range = new RedisZSetCommands.Range().gte(minScore).lte(maxScore);
        Set resp = setOperations.rangeByLex(zSet_test_key_1, range);
        log.info("rangeByLex resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }


    @ApiOperation("列表查询【根据(score)排序,正序返回「value」】- 查询条件【起始位置 start - 结束位置 end】")
    @RequestMapping(value = "/query/range",method = RequestMethod.GET)
    public BaseResp range(@RequestParam(value = "start",required = false,defaultValue = "0")Long start,@RequestParam(value = "end",required = false,defaultValue = "-1")Long end){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Set resp = setOperations.range(zSet_test_key_1,start,end);
        log.info("reverseRange resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation("列表查询(根据(score)排序,倒序返回「value」)- 查询条件【起始位置 start - 结束位置 end】")
    @RequestMapping(value = "/query/range/reverse",method = RequestMethod.GET)
    public BaseResp reverseRange(@RequestParam(value = "start",required = false,defaultValue = "0")Long start,@RequestParam(value = "end",required = false,defaultValue = "-1")Long end){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        Set resp = setOperations.reverseRange(zSet_test_key_1,start,end);
        log.info("reverseRange resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }


    @ApiOperation(value = "列表查询【根据(score)排序,正序返回「value」】- 查询条件【最小&最高】score")
    @RequestMapping(value = "/query/rangeByScore",method = RequestMethod.GET)
    public BaseResp rangeByScore(@RequestParam(value = "minScore",required = false)Double minScore,@RequestParam(value = "maxScore",required = false)Double maxScore){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        minScore = ObjectUtil.defaultIfNull(minScore,Double.MIN_VALUE);
        maxScore = ObjectUtil.defaultIfNull(maxScore,Double.MAX_VALUE);
        Set resp = setOperations.rangeByScore(zSet_test_key_1, minScore,maxScore);
        log.info("rangeByScore resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation(value = "列表查询(根据(score)排序,倒序返回「value」)-查询条件【最小&最高】score")
    @RequestMapping(value = "/query/rangeByScore/reverse",method = RequestMethod.GET)
    public BaseResp reverseRangeByScore(@RequestParam(value = "minScore",required = false)Double minScore,@RequestParam(value = "maxScore",required = false)Double maxScore){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        minScore = ObjectUtil.defaultIfNull(minScore,Double.MIN_VALUE);
        maxScore = ObjectUtil.defaultIfNull(maxScore,Double.MAX_VALUE);
        Set resp = setOperations.reverseRangeByScore(zSet_test_key_1, minScore,maxScore);
        log.info("reverseRangeByScore resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }


    @ApiOperation("列表查询(根据(score)排序,正序返回「score & value」 )- 查询条件【最小&最高】score")
    @RequestMapping(value = "/query/rangeByScoreWithScores",method = RequestMethod.GET)
    public BaseResp rangeByScoreWithScores(@RequestParam(value = "minScore",required = false)Double minScore,@RequestParam(value = "maxScore",required = false)Double maxScore){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        minScore = ObjectUtil.defaultIfNull(minScore,Double.MIN_VALUE);
        maxScore = ObjectUtil.defaultIfNull(maxScore,Double.MAX_VALUE);

        Set resp = setOperations.rangeByScoreWithScores(zSet_test_key_1, minScore,maxScore);
        log.info("rangeByScore resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }

    @ApiOperation("列表查询(根据(score)排序 倒序返回「score & value」)- 查询条件【最小&最高】score")
    @RequestMapping(value = "/query/rangeByScoreWithScores/reverse",method = RequestMethod.GET)
    public BaseResp reverseRangeByScoreWithScores(@RequestParam(value = "minScore",required = false)Double minScore,@RequestParam(value = "maxScore",required = false)Double maxScore){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        minScore = ObjectUtil.defaultIfNull(minScore,Double.MIN_VALUE);
        maxScore = ObjectUtil.defaultIfNull(maxScore,Double.MAX_VALUE);

        Set resp = setOperations.reverseRangeByScoreWithScores(zSet_test_key_1, minScore,maxScore);
        log.info("reverseRangeByScoreWithScores resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }


    String zSet_test_key_union_1 = "zSet_test_key_union_1";
    String zSet_test_key_union_2 = "zSet_test_key_union_2";
    String zSet_test_key_union_desc = "zSet_test_key_union_desc";

    @ApiOperation("多个有序集合,合并成一个新的有序集合(并集) 元素相同的score想加")
    @RequestMapping(value = "/store/unionAndStore",method = RequestMethod.GET)
    public BaseResp unionAndStore(){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        List users = TestDataUtil.getUsers();
        for (User user : users) {
            setOperations.add(zSet_test_key_union_1,user.getId(),user.getAge());
        }

        setOperations.add(zSet_test_key_union_2,1,10000);

        setOperations.add(zSet_test_key_union_2,-1,-1);

        Long resp = setOperations.unionAndStore(zSet_test_key_union_1,zSet_test_key_union_2,zSet_test_key_union_desc);
        log.info("unionAndStore resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }


    String zSet_test_key_intersect_1 = "zSet_test_intersect_1";
    String zSet_test_key_intersect_2 = "zSet_test_intersect_2";
    String zSet_test_key_intersect_desc = "zSet_test_intersect_dest";

    @ApiOperation("两个有序集合-交集, 两个有序集合 都有相同的元素,并且score相加")
    @RequestMapping(value = "/store/intersectAndStore",method = RequestMethod.GET)
    public BaseResp intersectAndStore(){
        ZSetOperations setOperations  = redisTemplate.opsForZSet();
        List users = TestDataUtil.getUsers();
        for (User user : users) {
            setOperations.add(zSet_test_key_intersect_1,user.getId(),user.getAge());
        }

        setOperations.add(zSet_test_key_intersect_2,1,100);
        setOperations.add(zSet_test_key_intersect_2,10003,100);

        Long resp = setOperations.intersectAndStore(zSet_test_key_intersect_1,zSet_test_key_intersect_2,zSet_test_key_intersect_desc);
        log.info("intersectAndStore resp:{}",resp);
        return BaseResp.buildDataSuccess(resp);
    }
    //-----------------redis zset end-------------
}

5、RedisTemplate支持常用的5种数据结构:String字符串、Hash哈希表、List列表、Set集合、Zset有序集合,方法对应的接口文档

在博客园看到大佬(昵称: 一天学点)把这5种数据结构 调用方法整理成了表格,我copy了一份备注了出处 方便后期查阅
1、使用ValueOperations操作redis(String字符串)
2、使用HashOperations操作redis(Hash哈希表)
3、使用ListOperations操作redis(List列表)
4、使用SetOperations(无序)操作redis(Set集合)
5、使用ZSetOperations(有序)操作redis(Zset有序集合)


6、随机创建用户信息的工具类(方便测试)

/****
 *
 * 

随机数生成工具类,主要包括
* 中文姓名,性别,Email,手机号,住址 * * @author youqiang.xiong * @date 2018年5月23日 下午2:15:31 * @version v1.0 * @ClassName: RandomValueUtil */ @Slf4j public class RandomValueUtil { public static String base = "abcdefghijklmnopqrstuvwxyz0123456789"; private static String firstName="赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解应宗宣丁贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄魏加封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘姜詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庚终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后江红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于仲孙太叔申屠公孙乐正轩辕令狐钟离闾丘长孙慕容鲜于宇文司徒司空亓官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓拔夹谷宰父谷粱晋楚阎法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况后有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福百家姓续"; private static String girl="秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 "; public static String boy="伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘"; public static final String[] email_suffix="@gmail.com,@yahoo.com,@msn.com,@hotmail.com,@aol.com,@ask.com,@live.com,@qq.com,@0355.net,@163.com,@163.net,@263.net,@3721.net,@yeah.net,@googlemail.com,@126.com,@sina.com,@sohu.com,@yahoo.com.cn".split(","); public static int getNum(int start,int end) { return (int)(Math.random()*(end-start+1)+start); } public static int getAge() { return getNum(1,100); } /*** * *

随机生成Email * * @param lMin * 最小长度 * @param lMax * 最大长度 * @return */ public static String getEmail(int lMin,int lMax) { int length=getNum(lMin,lMax); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = (int)(Math.random()*base.length()); sb.append(base.charAt(number)); } sb.append(email_suffix[(int)(Math.random()*email_suffix.length)]); return sb.toString(); } public static String getEmail(){ return getEmail(5,15); } private static String[] telFirst="134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(","); /*** * *

随机生成手机号码 * * @return */ public static String getTelephone() { int index=getNum(0,telFirst.length-1); String first=telFirst[index]; String second=String.valueOf(getNum(1,888)+10000).substring(1); String thrid=String.valueOf(getNum(1,9100)+10000).substring(1); return first+second+thrid; } /*** * * Project Name: recruit-helper-util *

随机生成8位电话号码 * * @since * @return */ public static String getLandline() { int index=getNum(0,telFirst.length-1); String first=telFirst[index]; String second=String.valueOf(getNum(1,888)+10000).substring(1); String thrid=String.valueOf(getNum(1,9100)+10000).substring(1); return first+second+thrid; } /** * 返回中文姓名 */ public static String name_sex = ""; /*** * *

返回中文姓名 * * @return */ public static String getChineseName() { int index = getNum(0, firstName.length() - 1); String first = firstName.substring(index, index + 1); int sex = getNum(0, 1); String str = boy; int length = boy.length(); if (sex == 0) { str = girl; length = girl.length(); name_sex = "女"; } else { name_sex = "男"; } index = getNum(0, length - 1); String second = str.substring(index, index + 1); int hasThird = getNum(0, 1); String third = ""; if (hasThird == 1) { index = getNum(0, length - 1); third = str.substring(index, index + 1); } return first + second + third; } public static void main(String[] args) { for (int i = 0; i < 200; i++) { String name =RandomValueUtil.getChineseName(); String phone = RandomValueUtil.getTelephone(); String email = RandomValueUtil.getEmail(5,15); Integer age = RandomValueUtil.getAge(); log.info("i:{} name:{} phone:{} email:{} age:{}",i,name,phone,email,age); } } }


代码我已经传到gitee了 传送门

你可能感兴趣的:(spring boot(三)整合 redis)