Spring boot 集成redis

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

Redis 是一个高性能的key-value内存数据库。它支持常用的5种数据结构:String字符串、Hash哈希表、List列表、Set集合、Zset有序集合 等数据类型。

Redis它解决了2个问题:

第一个是:性能

通常数据库的读操作,一般都要几十毫秒,而redisd的读操作一般仅需不到1毫秒。通常只要把数据库的数据缓存进redis,就能得到几十倍甚至上百倍的性能提升。

第二个是:并发

在大并发的情况下,所有的请求直接访问数据库,数据库会出现连接异常,甚至卡死在数据库中。为了解决大并发卡死的问题,一般的做法是采用redis做一个缓冲操作,让请求先访问到redis,而不是直接访问数据库。

一、添加maven依赖

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

二、配置文件

#端口
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<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> 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<User> set_pop(){
SetOperations<String, User> setOperations = redisTemplate.opsForSet();
User user= setOperations.pop(testSetKey);
return BaseResp.buildDataSuccess(user);
} @ApiOperation("randomMember 随机返回一个元素")
@RequestMapping(value = "/randomMember",method = RequestMethod.GET)
public BaseResp<User> set_randomMember(){
SetOperations<String, User> setOperations = redisTemplate.opsForSet();
User user= setOperations.randomMember(testSetKey);
return BaseResp.buildDataSuccess(user);
} @ApiOperation("添加一个元素")
@RequestMapping(value = "/add",method = RequestMethod.GET)
public BaseResp<Long> set_add(@RequestParam("setKey")String setKey,@RequestParam("setVal")String setVal){
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
Long resp = setOperations.add(setKey,setVal);
return BaseResp.buildDataSuccess(resp);
} @ApiOperation("判断一个元素是否存在")
@RequestMapping(value = "/isMember",method = RequestMethod.GET)
public BaseResp<Boolean> set_isMember(@RequestParam("setKey")String setKey,@RequestParam("setVal")String setVal){
SetOperations<String, User> 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<String, User> setOperations = redisTemplate.opsForSet();
return BaseResp.buildDataSuccess(setOperations.size(setKey));
} @ApiOperation("多个set-差集(取第一个set集合中 在其他set集合没有出现的元素)")
@RequestMapping(value = "/difference",method = RequestMethod.GET)
public BaseResp list_difference(){
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
Set<String> 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<String, String> setOperations = redisTemplate.opsForSet(); Set<String> 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<String, String> setOperations = redisTemplate.opsForSet();
Set<String> 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<Set> hash_keys(@RequestParam("h")String h){
return BaseResp.buildDataSuccess(redisTemplate.opsForHash().keys(h));
} @ApiOperation("keys方法")
@RequestMapping(value = "/delete",method = RequestMethod.GET)
public BaseResp<Long> 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<User> 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<User> users = TestDataUtil.getUsers(); Set<ZSetOperations.TypedTuple<User>> 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<User> 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<User> 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<User> 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、随机创建用户信息的工具类(方便测试)

/****
*
* <p>随机数生成工具类,主要包括<br>
* 中文姓名,性别,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);
}
/***
*
* <p>随机生成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(","); /***
*
* <p>随机生成手机号码
*
* @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
* <p>随机生成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 = ""; /***
*
* <p>返回中文姓名
*
* @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的更多相关文章

  1. Spring Boot (二) 整合 Redis

    前言 本文将会基于 springboot 2.1.8.RELEASE 简单整合 Redis ,适合新手小白入门 Spring Boot 整合 Redis 入门 1.pom.xml 中引入 redis ...

  2. Spring Boot 2整合Redis做缓存

    既然是要用Redis做缓存,自然少不了安装了.但是本文主要讲Spring Boot与Redis整合.安装教程请另行百度! 1.首先是我们的Redis配置类 package com.tyc; impor ...

  3. Spring boot(三)整合mybaties+thymeleaf实现基础crud

    工程结构: 首先在pom文件中引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  4. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  5. Spring Boot:使用Redis存储技术

    综合概述 Redis是一个开源免费的高性能key-value数据库,读取速度达110000次/s,写入速度达81000次/s.Redis支持丰富的数据类型,如Lists, Hashes, Sets 及 ...

  6. Spring Boot 应用系列 5 -- Spring Boot 2 整合logback

    上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对 ...

  7. Spring Boot:整合Spring Security

    综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Author ...

  8. Spring Boot:整合Spring Data JPA

    综合概述 JPA是Java Persistence API的简称,是一套Sun官方提出的Java持久化规范.其设计目标主要是为了简化现有的持久化开发工作和整合ORM技术,它为Java开发人员提供了一种 ...

  9. Spring Boot:整合Shiro权限框架

    综合概述 Shiro是Apache旗下的一个开源项目,它是一个非常易用的安全框架,提供了包括认证.授权.加密.会话管理等功能,与Spring Security一样属基于权限的安全框架,但是与Sprin ...

  10. Spring Boot 中集成 Redis 作为数据缓存

    只添加注解:@Cacheable,不配置key时,redis 中默认存的 key 是:users::SimpleKey [](1.redis-cli 中,通过命令:keys * 查看:2.key:缓存 ...

随机推荐

  1. 9.亿级流量电商系统JVM模型参数预估方案

    1. 需求分析 大促在即,拥有亿级流量的电商平台开发了一个订单系统,我们应该如何来预估其并发量?如何根据并发量来合理配置JVM参数呢? 假设,现在有一个场景,一个电商平台,比如京东,需要承担每天上亿的 ...

  2. Java基础之(二):Notepad++实现HelloWorld

    现在我们开始编写我们的第一个程序:Hello World! HelloWorld 新建一个java文件 文件后缀名为.java Hello.java 代码分析: 接下来写完最大的框之后,那接下来当然就 ...

  3. 2020.10.10--pta阶梯赛练习2补题

    7-3.N个数求和 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式. 输入格式: 输入第一行给出一个正整数N(≤100).随后 ...

  4. Java(45)JDK新特性之String流

    作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15201671.html 博客主页:https://www.cnblogs.com/testero ...

  5. sql常用的统计公式

    hivesql中max,min函数不能作用于多列,因此在有上下门限区间限制时多用公式直接计算. max(x,y)=(x+y+ABS(x-y))/2 min(x,y)=(x+y-ABS(x-y))/2 ...

  6. '\r'(回车符),'\n'(换行符)与"\r\n"

    一.'\n','\r'和"\r\n" 回车\r本义是光标重新回到本行开头,r的英文return,控制字符可以写成CR,即Carriage Return(回车,carriage有&q ...

  7. 剑指offer:JZ12 矩阵中的路径

    JZ12 矩阵中的路径 描述 请设计一个函数,用来判断在一个n乘m的矩阵中是否存在一条包含某长度为len的字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上, ...

  8. 《Spring源码深度解析》学习笔记——Spring的整体架构与容器的基本实现

    pring框架是一个分层架构,它包含一系列的功能要素,并被分为大约20个模块,如下图所示 这些模块被总结为以下几个部分: Core Container Core Container(核心容器)包含有C ...

  9. Scrum Meeting 0503

    零.说明 日期:2021-5-3 任务:简要汇报两日内已完成任务,计划后两日完成任务 一.进度情况 组员 负责 两日内已完成的任务 后两日计划完成的任务 qsy PM&前端 完成登录.后端管理 ...

  10. Noip模拟18 2021.7.17 (文化课专场)

    T1 导弹袭击(数学) 显然,我们要找到最优的A,B使得一组a,b优于其他组那么可以列出: $\frac{A}{a_i}+\frac{B}{b_i}<\frac{A}{a_j}+\frac{B} ...