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 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 string2Obj(String str,Class 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);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
