生活随笔
收集整理的這篇文章主要介紹了
Redis工具类封装讲解和实战
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Redis工具類封裝講解和實(shí)戰(zhàn)
?? ?簡(jiǎn)介:高效開(kāi)發(fā)方式 Redis工具類封裝講解和實(shí)戰(zhàn)
?? ??? ?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();//對(duì)象轉(zhuǎn)字符串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;}}//字符串轉(zhuǎn)對(duì)象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/*** 功能描述:設(shè)置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;}} /*** 功能描述:通過(guò)key獲取緩存里面的值* @param key* @return*/public String get(String key){return redisTpl.opsForValue().get(key);}// @Autowired
// private StringRedisTemplate redisTemplate;
//
//
// /**
// * 通過(guò)字符串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;
// }
//
// }
// //
// /**
// * 功能描述:設(shè)置某個(gè)key過(guò)期時(shí)間
// * @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;
// }
// }
//
//
//
//
// /**
// * 功能描述:根據(jù)key 獲取過(guò)期時(shí)間
// * @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結(jié)構(gòu)=====================
//
//
// //==============List結(jié)構(gòu)=====================
//
//
// }
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})//啟動(dòng)整個(gè)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);}}
?
總結(jié)
以上是生活随笔為你收集整理的Redis工具类封装讲解和实战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。