1.使用jedis操作redis
创建jedis对象,连接redis数据库
使用jedis对象调用不同redis类型数据方法
例如操作String类型
String host="192.168.101.20";
int port=6379;
//连接redis数据库
Jedis jedis=new Jedis(host, port);
//redis数据库保存字符串数据
//jedis.set("name", "wangwu");
//获得redis数据的数据
//String result = jedis.get("name");
//System.out.println("result="+result);
//给指定的key对应的值,进行追加
//jedis.append("name", " 北京");
//进行字符串的截取
//String result2 = jedis.substr("name", 0, 3);
//System.out.println("result2="+result2);
//一次添加多个字符串的键值对
//jedis.mset("sno","100100","sname","xiaoqiang","address","beijing");
//指定多个键,获取多个值
/*List<String> list = jedis.mget("sno","sname","address");
System.out.println("list="+list);*/
//给指定的key,对于的值添加一个增量
//jedis.incrBy("sno", 1);
//统计字符串长度,汉子按照字节长度统计
Long len = jedis.strlen("name");
System.out.println("len="+len);
//修改某个key对应的值
String old = jedis.getSet("sname", "wangcai");
System.out.println("old="+old);
2.使用spring封装的RedisTemplate操作数据库缓存
添加依赖
<!--spring-cache的依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.13.RELEASE</version>
</dependency>
<!--jedis依赖-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 添加jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
配置xml文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 配置springcache,当前缓存功能由redis提供 -->
<!-- 实例化RedisTemplate对象
spring提供的操作Redis数据的模板对象
-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<!-- 注入redis的连接工厂 -->
<property name="connectionFactory" ref="redisConnectionFactory"></property>
<!-- 注入key的序列化方式 -->
<property name="keySerializer" ref="keySer"></property>
<!-- 注入value的序列化方式 -->
<property name="valueSerializer" ref="valSer"></property>
</bean>
<!-- 实例化key的序列化对象 -->
<bean id="keySer" class="org.springframework.data.redis.serializer.GenericToStringSerializer">
<!-- 这里调用带参数构造方法,参数的类型是Class,传入的一个类的完全限定名,通过完全限定名可以获取class -->
<constructor-arg name="type" value="java.lang.String"></constructor-arg>
</bean>
<!-- 实例化value的序列化对象 -->
<bean id="valSer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer">
</bean>
<!-- 实例化Redis数据库的连接工厂 -->
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.15.20"></property>
<property name="port" value="6379"></property>
<!-- <property name="password" value="123456"></property> -->
</bean>
</beans>
创建service实现类
* template对象操作redis数据库:
* 默认的序列化方式:byte数组,实体类需要实现序列化接口
@Autowired
private RedisTemplate template;
@Override
public List<Car> loadCarListService() {
Object obj = template.boundValueOps("jsonList").get();
if(obj!=null){
return (List<Car>) obj;
}
System.out.println("查询数据库");
obj = carMapper.loadCarList();
template.boundValueOps("jsonList").set(obj);;
return (List<Car>) obj;
}