redis是一种非关系形数据库,使用key,value形式存储或去除数据。做为缓存可以对其做持久化或者其他操作。
REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统
它通常被称为数据结构服务器,因为值(value)可以是 字符串(String), 哈希(Hash), 列表(list), 集合(sets) 和 有序集合(sorted sets)等类型。
注:
一般情况下默认下载的redis是linux系统下的
redis配置
**1,在配置文件中写明一系列配置信息**
spring.redis.database=0
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=123456
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-idle=-1
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-active=8
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
#连接超时时间(毫秒)
spring.redis.timeout=0
spring.redis.block-when-exhausted=true
redis连接所需要的实体类,在自动注入的时候回自动获取配置文件中的属性
@Component
@ConfigurationProperties(prefix = "spring.redis")
//这里注意上面的导入spring.redis的注解,需要在pom文件中引入。才会自动注入。
public class RedisConn {
private String host;
private int port;
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = TripleDESEncrypt.decrypt(password);
}
}
创建RedisConfig的类这里自动注入上面创建的实体类
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.lang.reflect.Method;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Autowired
private RedisConn redisConn;
/**
* 生产key的策略
*
* @return
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
}
};
}
/**
* redis 数据库连接池
*
* @return
*/
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(redisConn.getHost());
factory.setPort(redisConn.getPort());
factory.setPassword(redisConn.getPassword());
return factory;
}
/**
* redisTemplate配置
*
* @param factory
* @return
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(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);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
上面配置了 pom yml,还有自动获取一个实体类对象,以及创建redis连接池的对象这里再创建一个redis的工具类用来具体操作
里面的两个方法是操作hash类型的存储方式的,并且都加入了选库存储读取(读取)。如果使用其他方式的在网上百度具体的方法
注:这是RedisTemplate的各种操作,对于一些常用类型的操作
https://blog.csdn.net/lichuangcsdn/article/details/80866182
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class RedisUtil {
@SuppressWarnings("rawtypes")
@Autowired
private RedisTemplate redisTemplate;
/**
* 读取缓存,根据固定的
* 输入对应的key redis库编号以及hashname
* 查询对应的value
* @param key,dataBaseIndex,hashName
* @return
*/
@SuppressWarnings("unchecked")
public Object get(final String key,int dataBaseIndex,String hashName) {
JedisConnectionFactory connectionFactory =(JedisConnectionFactory) redisTemplate.getConnectionFactory();
connectionFactory.setDatabase(dataBaseIndex);//选库环节
String value=(String)redisTemplate.opsForHash().get(hashName,key);
return value;
}
public void set(Map map,int databaseindex,String hashname) {
JedisConnectionFactory connectionFactory =(JedisConnectionFactory) redisTemplate.getConnectionFactory();//选库环节
connectionFactory.setDatabase(databaseindex);//选库环节
redisTemplate.opsForHash().putAll(hashname,map); ;
}
}
下面是一个小型demo可以参照一下
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
/**
* @author flx
* @version V1.0
* @date 2019/8/7 10:50
* @Copyright © 中兴力维
*/
@RestController
@RequestMapping(value="/redis")
public class RedisController {
@Autowired
private RedisUtil redisUtil;
@RequestMapping(value = "getRedis")
@ResponseBody
public void getRedis(){
String hashname="test:001";
String res =(String) redisUtil.get("2",3,hashname);
System.out.println(res);
Map map=new HashMap<String,String>();
map.put("1","1");
map.put("2","2");
map.put("3","3");
redisUtil.set(map,3,"test:002");
}
}