非关系型数据库Redis + Spring整合Redis
2017-12-14 本文已影响0人
writeanewworld
1.简介
数据库持久化数据主要是面向磁盘,磁盘的读写速度是比较慢的。在互联网中往往存在大量的数据请求,这种数据库承受不住高并发访问,服务器易宕机。
为了克服这些问题。javaweb往往引用NoSql技术。 NoSql(Redis MongoDb),是一种基于内存的简易数据库,提供一定的持久化功能。性能优越支持集群,分布式。有一定的事务能力,所以在高并发的情况下也可以保证数据的安全、一致。
2.安装Redis,写一个小程序简单测试一下电脑性能
下载Redis安装配置、新建java项目,导入jar包。
package com.begin21.PerformanceTest;
import redis.clients.jedis.Jedis;
public class Test {
// 首先开启本机的Redis服务 redis-server.exe redis.windows.conf redis-cli.exe
public static void main(String[] args) {
//链接redis
Jedis jedis = new Jedis("localhost",6379);
//记录操作次数
int count = 0;
//密码
//jedis.auth("");
try {
//开始毫秒数
long start = System.currentTimeMillis();
while(true){
//结束毫秒数
long end = System.currentTimeMillis();
//大于1秒结束操作
if((end-start) >= 1000){
break;
}
count++;
jedis.set("test"+count , count+"");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭数据库链接
jedis.close();
}
System.out.println("redis每秒操作" + count + "次");
}}
性能测试:
image.png
只能说,贼垃圾
3Sping+Redis.
Redis中只提供了对String的操作,java以类对象为主,需要redis存储的字符串和java对象相互转换。
Spring对此进行了封装和支持,提供序列化的设计框架和一些序列化的类,通过序列化把java对象转化,redis存储起来。读取时,再由序列化过的字符串转化为对象。
4.步骤
配置数据库连接池
配置Spring提供的连接工厂
对象序列化
同一redis连接
配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 配置JedisPoolConfig连接池对象 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 配置最大空闲数 -->
<property name="maxIdle" value="50" />
<!-- 配置最大连接数 -->
<property name="maxTotal" value="100" />
<!-- 配置最大等待时间 -->
<property name="maxWaitMillis" value="20000" />
</bean>
<!-- 配置JedisConnectionFactory spring提供的链接工厂-->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"/>
<property name="port" value="6379"/>
<property name="poolConfig" ref="poolConfig"/>
</bean>
<!-- 对象序列化 -->
<!-- 键序列器 -->
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
<!-- 值序列器 -->
<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
<!-- 同一Redis连接-->
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="keySerializer" ref="stringRedisSerializer"/>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"/>
</bean>
</beans>
Main:
package com.begin21.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import com.begin21.pojo.Role;
public class RoleMain {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
testRedisl();
}
@SuppressWarnings("unchecked")
public static void testRedisl() {
applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
// 反射获取jar包中的RedisTemplate
@SuppressWarnings({ "rawtypes" })
RedisTemplate redisTemplate = applicationContext
.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1);
role.setRoleName("王凯");
role.setNote("wangkai");
// 序列化放到redis中
redisTemplate.opsForValue().set("role1", role);
SessionCallback<Role> callback = new SessionCallback<Role>() {
@Override
public Role execute(
@SuppressWarnings("rawtypes") RedisOperations ops)
throws DataAccessException {
ops.boundValueOps("role1").set(role);
return (Role) ops.boundValueOps("role1").get();
}
};
Role savedRole = (Role) redisTemplate.execute(callback);
System.out.println(savedRole.getRoleName()+savedRole.getNote());
}
}