10-springboot整合redis

}

}

自定义RedisTemplate类


package com.tian.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration

public class RedisConfig {

//这是一个固定模板,拿去就可以直接使用

@Bean

@SuppressWarnings(“all”)

public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){

RedisTemplate<String, Object> template = new RedisTemplate<>();

template.setConnectionFactory(factory);

Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

ObjectMapper om = new ObjectMapper();

om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

jackson2JsonRedisSerializer.setObjectMapper(om);

StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

//key采用String的序列化方式

template.setKeySerializer(stringRedisSerializer);

//hash的key也采用String的序列化方式

template.setHashKeySerializer(stringRedisSerializer);

//value序列化方式采用jackson

template.setValueSerializer(jackson2JsonRedisSerializer);

//hash的value序列化采用jackson

template.setHashKeySerializer(jackson2JsonRedisSerializer);

template.afterPropertiesSet();

return template;

}

}

pojo

public class User implements Serializable {

private String name;

private int age;

}

测试

@Autowired

@Qualifier(“redisTemplate”)

private RedisTemplate redisTemplate;

@Test

public void test() throws JsonProcessingException {

//真实的开发一般都使用json来传递对象

User user = new User(“狂神说”, 3);

// String jsonUser = new ObjectMapper().writeValueAsString(user);

redisTemplate.opsForValue().set(“user”,user);

System.out.println(redisTemplate.opsForValue().get(“user”));

}

//没有自定义redisTemplate的话,保存的user是一对看不懂的字符串,自定义的话,保存的就是user

工具类

package com.tian.utils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Component;

import org.springframework.util.CollectionUtils;

import java.util.Collection;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.concurrent.TimeUnit;

@Component

public class RedisUtil {

//自己的redisTemplate

@Autowired

private RedisTemplate<String,Object> redisTemplate;

public boolean expire(String key,long time){

try{

if (time>0){

redisTemplate.expire(key,time, TimeUnit.SECONDS);

}

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public long getExpire(String key){

return redisTemplate.getExpire(key,TimeUnit.SECONDS);

}

public boolean hasKey(String key){

try{

return redisTemplate.hasKey(key);

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public void del(String… key){

if(key!=null&&key.length>0){

if(key.length==1){

redisTemplate.delete(key[0]);

}else {

redisTemplate.delete((Collection) CollectionUtils.arrayToList(key));

}

}

}

public Object get(String key){

return key==null?null:redisTemplate.opsForValue().get(key);

}

public boolean set(String key,Object value){

try{

redisTemplate.opsForValue().set(key,value);

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public boolean set(String key,Object value,long time){

try{

if(time>0){

redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);

}else {

set(key,value);

}

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public long incr(String key,long delta){

if(delta<0){

throw new RuntimeException(“递增因子必须大于0”);

}

return redisTemplate.opsForValue().increment(key, delta);

}

public long decr(String key,long delta){

if(delta<0){

throw new RuntimeException(“递减因子必须大于0”);

}

return redisTemplate.opsForValue().increment(key, -delta);

}

public Object hget(String key,String item){

return redisTemplate.opsForHash().get(key,item);

}

public Map<Object,Object> hmget(String key){

return redisTemplate.opsForHash().entries(key);

}

public boolean hmset(String key,Map<String,Object>map){

try{

redisTemplate.opsForHash().putAll(key,map);

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public boolean hmget(String key,Map<String,Object>map,long time){

try{

redisTemplate.opsForHash().putAll(key,map);

if(time>0){

expire(key,time);

}

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public boolean hset(String key,String item,Object value){

try{

redisTemplate.opsForHash().put(key,item,value);

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public boolean hset(String key,String item,Object value,long time){

try{

redisTemplate.opsForHash().put(key,item,value);

if(time>0){

expire(key, time);

}

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public void hdel(String key,Object… item){

redisTemplate.opsForHash().delete(key, item);

}

public boolean hHasKey(String key,String item){

return redisTemplate.opsForHash().hasKey(key,item);

}

public double hincr(String key,String item,double by){

return redisTemplate.opsForHash().increment(key,item,by);

}

public double hdecr(String key,String item,double by){

return redisTemplate.opsForHash().increment(key,item,-by);

}

public Set sGet(String key){

try{

return redisTemplate.opsForSet().members(key);

}catch (Exception e){

e.printStackTrace();

return null;

}

}

public boolean sHasKey(String key,Object value){

try{

return redisTemplate.opsForSet().isMember(key,value);

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public long sSet(String key,Object… value){

try{

return redisTemplate.opsForSet().add(key, value);

}catch (Exception e){

e.printStackTrace();

return 0;

}

}

public long sSetAndTime(String key,long time,Object… value){

try{

long count= redisTemplate.opsForSet().add(key, value);

if(time>0){

expire(key,time);

}

return count;

}catch (Exception e){

e.printStackTrace();

return 0;

}

}

public long sGetSetSize(String key){

try{

return redisTemplate.opsForSet().size(key);

}catch (Exception e){

e.printStackTrace();

return 0;

}

}

public long setRemove(String key,Object… values){

try{

long count=redisTemplate.opsForSet().remove(key, values);

return count;

}catch (Exception e){

e.printStackTrace();

return 0;

}

}

public List Ilist(String key,long start,long end){

try{

return redisTemplate.opsForList().range(key,start,end);

}catch (Exception e){

e.printStackTrace();

return null;

}

}

public Object lGetIndex(String key,long index){

try{

return redisTemplate.opsForList().index(key, index);

}catch (Exception e){

e.printStackTrace();

return null;

}

}

public boolean lSet(String key,Object value){

try{

redisTemplate.opsForList().rightPush(key, value);

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public boolean lSet(String key,Object value,long time){

try{

redisTemplate.opsForList().rightPush(key, value);

if(time>0){

expire(key, time);

}

return true;

}catch (Exception e){

e.printStackTrace();

return false;

}

}

public boolean lSet(String key,List value){

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
urn false;

}

}

public boolean lSet(String key,List value){

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-ENbu3iMT-1715586240847)]

[外链图片转存中…(img-rA2mE1D5-1715586240848)]

[外链图片转存中…(img-OtYtit87-1715586240848)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 24
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值