SpringMvc 4.3 整合redis,springmvcredis
本篇仅限自己备忘,因框架配置不尽相同,其他人仅供参考!
主要修改以下文件:
pom.xml
redis.properties
springMvc-base.xml
pom.xml
添加redis依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.1.0</version>
</dependency>
redis.properties
# Redis settings
redis.host=192.168.5.99
redis.pass=
redis.port =6379
redis.maxIdle =300
redis.default.db =0
redis.timeout =100000
redis.maxActive =600
redis.maxWait =1000
redis.testOnBorrow =true
timeout 默认是6000 即6秒,我改成了100秒,因为我写了个测试,20个线程,每个线程负责存储30万条hash,结果每次到200多万就会报与redis链接超时,所以我就改大了。实际测试600万数据插入耗时4分半,后来无论怎么增加线程数量,都不会再有效率提升,在linux测试更快一些。我猜想是受限于windows 下cpu的性能。
springMvc-base.xml
因为需要即支持物理数据库,又要支持内存数据库,所以需要变更以下配置
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:Jdbc/jdbc.properties" />
</bean>
变更为:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!--<property name="location" value="classpath:Jdbc/jdbc.properties" />-->
<property name="locations">
<list>
<value>classpath:Jdbc/jdbc.properties</value>
<value>classpath:redis/redis.properties</value>
</list>
</property>
</bean>
增加如下配置:
<!-- redis缓存数据库的相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxActive" value="${redis.maxActive}" />
<property name="maxWait" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:password="${redis.pass}"
p:pool-config-ref="poolConfig"
p:timeout="${redis.timeout}"
/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="LoginRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
测试:
controller
/**
* 保存用户
* @param -name
* @param- sex
* @param -age
* @return
*/
@RequestMapping("/save")
@ResponseBody
public Integer save(@RequestParam(value="userName", defaultValue="") String userName,
@RequestParam(value="password", defaultValue="") String password){
System.err.println("name: ----------"+userName );
service.saveUser(userName, password);
return 1;
}
service (我的框架里面没有dao,懒的写。项目小,没啥用。技术就这么烂,唉没办法)
@Resource
protected RedisTemplate<String, Object> redisTemplate;
@Override
public void saveUser(String userName, String pwd) {
for (int i = 0; i <20 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
Long a =System.currentTimeMillis();
System.out.println("start===="+(a)/1000);
for (int i = 0; i <300000 ; i++) {
String uuid = UUID.randomUUID().toString().replace("-","");
redisTemplate.opsForValue().set(uuid,"liushuai");
}
System.out.println("end===="+(System.currentTimeMillis()-a)/1000);
}
}).start();
}
Map<String,String> maps = new HashMap<String, String>();
maps.put("multi1","multi1");
maps.put("multi2","multi2");
maps.put("multi3","multi3");
redisTemplate.opsForValue().multiSet(maps);
List<String> keys = new ArrayList<String>();
keys.add("multi1");
keys.add("multi2");
keys.add("multi3");
System.out.println(redisTemplate.opsForValue().multiGet(keys));
}
以上就是集成过程了
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。