Redis工具类封装讲解和实战
Redis工具类封装讲解和实战
简介:高效开发方式 Redis工具类封装讲解和实战
1、常用客户端 https://redisdesktop.com/download
2、封装redis工具类并操作
package net.leon.base_project.utils;import java.io.IOException;import org.springframework.util.StringUtils;import com.fasterxml.jackson.databind.ObjectMapper;public class JsonUtils {private static ObjectMapper objectMapper = new ObjectMapper();//对象转字符串public static <T> String obj2String(T obj){if (obj == null){return null;}try {return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);} catch (Exception e) {e.printStackTrace();return null;}}//字符串转对象public static <T> T string2Obj(String str,Class<T> clazz){if (StringUtils.isEmpty(str) || clazz == null){return null;}try {return clazz.equals(String.class)? (T) str :objectMapper.readValue(str,clazz);} catch (IOException e) {e.printStackTrace();return null;}}
}
package net.leon.base_project.utils;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;/*** 功能描述:redis工具类*/
@Component
public class RedisClient {@Autowiredprivate StringRedisTemplate redisTpl; //jdbcTemplate/*** 功能描述:设置key-value到redis中* @param key* @param value* @return*/public boolean set(String key ,String value){try{redisTpl.opsForValue().set(key, value);return true;}catch(Exception e){e.printStackTrace();return false;}} /*** 功能描述:通过key获取缓存里面的值* @param key* @return*/public String get(String key){return redisTpl.opsForValue().get(key);}// @Autowired
// private StringRedisTemplate redisTemplate;
//
//
// /**
// * 通过字符串key获取值
// * @param key 键
// * @return 值
// */
// public String get(String key){
// return key==null?null:redisTemplate.opsForValue().get(key);
// }
//
//
// /**
// * 普通缓存放入
// * @param key 键
// * @param value 值
// * @return true成功 false失败
// */
// public boolean set(String key,String value) {
// try {
// redisTemplate.opsForValue().set(key, value);
// return true;
// } catch (Exception e) {
// e.printStackTrace();
// return false;
// }
//
// }
// //
// /**
// * 功能描述:设置某个key过期时间
// * @param key
// * @param time
// * @return
// */
// 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;
// }
// }
//
//
//
//
// /**
// * 功能描述:根据key 获取过期时间
// * @param key
// * @return
// */
// public long getExpire(String key){
// return redisTemplate.getExpire(key,TimeUnit.SECONDS);
// }
//
//
// /**
// * 递增
// * @param key 键
// * @return
// */
// public long incr(String key, long delta){
// return redisTemplate.opsForValue().increment(key, delta);
// }
//
//
// /**
// * 递减
// * @param key 键
// * @param delta 要减少几
// * @return
// */
// public long decr(String key, long delta){
// return redisTemplate.opsForValue().increment(key, -delta);
// }
//
// //==============Map结构=====================
//
//
// //==============List结构=====================
//
//
// }
package net.leon.base_project.controller;import java.util.Date;import net.leon.base_project.domain.JsonData;
import net.leon.base_project.domain.User;
import net.leon.base_project.utils.JsonUtils;
import net.leon.base_project.utils.RedisClient;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/v1/redis")
public class RdisTestController {@Autowiredprivate StringRedisTemplate redisTpl; //jdbcTemplate@Autowiredprivate RedisClient redis;@GetMapping(value="add")public Object add(){//redisTpl.opsForValue().set("name", "leon2018");redis.set("username", "xddddddd");return JsonData.buildSuccess();}@GetMapping(value="get")public Object get(){//String value = redisTpl.opsForValue().get("name");String value = redis.get("username");return JsonData.buildSuccess(value);}@GetMapping(value="save_user")public Object saveUser(){User user = new User(1, "abc", "11", new Date());String userStr = JsonUtils.obj2String(user);boolean flag = redis.set("base:user:11", userStr);return JsonData.buildSuccess(flag);}@GetMapping(value="find_user")public Object findUser(){String userStr = redis.get("base:user:11");User user = JsonUtils.string2Obj(userStr, User.class);return JsonData.buildSuccess(user);}}
package base_project.base;import net.leon.base_project.leonApplication;
import net.leon.base_project.domain.User;
import net.leon.base_project.utils.JsonUtils;
import net.leon.base_project.utils.RedisClient;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class) //底层用junit SpringJUnit4ClassRunner
@SpringBootTest(classes={Application.class})//启动整个springboot工程
public class JsonTest {@Autowiredprivate StringRedisTemplate strTpl;@Autowiredprivate RedisClient redis;@Testpublic void testOne(){User u = new User();u.setAge(1);u.setPhone("22222");u.setPwd("0000"); String str = JsonUtils.obj2String(u);strTpl.opsForValue().set("str", str);System.out.println(str);}}
Redis工具类封装讲解和实战相关推荐
- Redis工具类封装
Redis工具类封装 使用redis也好几年了,总是拷贝来拷贝去的,这次干脆放在这把,每次来这拷贝,不用在工程里面找来找去了. /*** Redis工具类* @author Huangliniao* ...
- Redis工具类封装RedisUtils
本文参考:https://blog.it-follower.com/posts/2563248908.html SpringBoot项目集成Redis相当简单,只需要pom中加入对应依赖 <de ...
- SpringBoot Redis工具类封装
SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable).看了很多博客后, ...
- Redis工具类封装RedisUtils(两种)
RedisTemplate工具类1 本文参考:https://blog.it-follower.com/posts/2563248908.html SpringBoot项目集成Redis相当简单,只需 ...
- redis配置类+redis工具类
1.RedisConfig.java类 import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jac ...
- IT忍者神龟之Redis操作工具类封装
Redis操作字符串工具类封装,Redis工具类封装 Java代码 package com.lqy.spring.redis; import redis.clients.jedis.Jedis; ...
- Redis工具类的封装
Redis工具类的封装 <dependency><groupId>redis.clients</groupId><artifactId>jedis< ...
- 【Spring之轨迹】结合 @Scheduled 实现定时将 Redis 缓存刷入数据库(配 Redis 工具类与例子实战)
-- 目录 -- 0. 假设已配置好 SSM 环境 1. 配置文件 2. 定时服务 3. cron 解释 ① cron 参数(按顺序依次为) ② 特殊符号 4. Redis 工具类 5. 例子实战 0 ...
- Redis(七) - 封装Redis工具类
文章目录 一.封装Redis工具类 1. 使用构造方法注入StringRedisTemplate 2. 方法1:将任意Java对象序列化为json并存储在string类型的key中,并且可以设置TTL ...
最新文章
- 关键词优化一定要从正规渠道入手
- Excel导出多sheet单sheet通用型(poi)
- php 检测密码,php如何检测账号密码是否匹配
- lnmp php fpm 默认,LNMP(PHP-FPM)
- k8s通过label来控制pod的位置
- php中的div是什么意思,div是什么意思?div标签怎么用
- 关于for循环中的变量int i 如果跳出了这个for循环后,i的值是继续保留还是被释放掉了
- python调研报告总结体会_调研报告心得体会
- 一个函数中写多少行代码比较合适呢?
- 数据库工程师基础学习1----计算机硬件基础知识,计算机体系结构与存储系统
- echarts实现山东地图可放大缩小可下钻
- mybatis插件助手的使用
- HEG安装教程以及闪退问题解决
- nginx 过滤某个url请求
- 计算机安装msvcr110.dll,帮您还原win7系统运行程序提示计算机中丢失msvcr110.dll的解决方法...
- 多进程log4j日志丢失问题分析
- 江苏计算机对口单招本科学校,江苏对口单招本科学校
- Nginx的优化配置
- 访问网站时,长时间打不开或无响应
- android p 荣耀v10,荣耀V10和V9哪个好 区别对比分析告诉你