redies项目构建
项目需要redies 搭建时候小记
通过@value注解读取springbootyml配置文件中redies的配置
- host 对应rediesip
- port 对应redies端口 默认6379
- timeout 对应超时时间
- password 对应密码
- dataBase 对应redies存储分区 一共16个分区(0-15)默认分区为0
创建对象即可,new时候不需要传参,图片为练习,所以传参了
===================================================================================
springboot集成redies RedisTemplate 及工具类
RedisService接口
package com.yqjr.stnd.common;
public interface RedisService {
/**
* set存数据
* @param key
* @param value
* @return
*/
boolean set(String key, String value);
/**
* get获取数据
* @param key
* @return
*/
String get(String key);
/**
* 设置有效天数
* @param key
* @param expire
* @return
*/
boolean expire(String key, long expire);
/**
* 移除数据
* @param key
* @return
*/
boolean remove(String key);
}
RedisService接口实现类
package com.yqjr.stnd.common;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;
@Service
public class RedisServiceImpl implements RedisService {
@Resource
private RedisTemplate<String, ?> redisTemplate;
@Override
public boolean set(final String key, final String value) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
connection.set(serializer.serialize(key), serializer.serialize(value));
return true;
}
});
return result;
}
@Override
public String get(final String key) {
String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] value = connection.get(serializer.serialize(key));
return serializer.deserialize(value);
}
});
return result;
}
@Override
public boolean expire(final String key, long expire) {
return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public boolean remove(final String key) {
boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
connection.del(key.getBytes());
return true;
}
});
return result;
}
}
rest测试
package com.yqjr.stnd.web.rest;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.yqjr.stnd.common.RedisService;
import redis.clients.jedis.JedisPool;
import java.util.HashMap;
import java.util.Map;
@RequestMapping("/redisUtil")
@RestController
@RunWith(SpringRunner.class)
public class RedisController {
@Autowired
private RedisService redisService;
/**
* redis添加测试
*
* @param redisKey
* @param redisValue
*/
@RequestMapping("addRedis")
@ResponseBody
public String addRedis(String redisKey, String redisValue) {
String result = redisService.get(redisKey);
return result;
}
}