17.2.1 在Windows下安装Redis

https://github.com/ServiceStack/redis-windows/tree/master/downloads

redis-server redis.windows.conf

17.3.1 在Java程序中使用Redis

http://mvnrepository.com/artifact/redis.clients/jedis/2.9.0

Java连接Redis

package com.ssm.chapter17.jedis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; public class JedisTest {
public void testJdedis(){
Jedis jedis = testPool().getResource(); //Jedis jedis = new Jedis("localhost", 6379);//连接Redis
//jedis.auth("password");//如果需密码 int i =0;//记录操作次数
try{
long start = System.currentTimeMillis();// 开始毫秒数
while(true){
long end = System.currentTimeMillis();
if (end-start >= 1000) {// 当大于等于1000毫秒(相当于1秒)时,结束操作
break; }
i++;
jedis.set("test" + i, i + "");
}
} finally{// 关闭连接
jedis.close();
}
System.out.println("redis每秒操作:" + i + "次");//打印1秒内对Redis的操作次数 }
}

使用Redis连接池

  private JedisPool testPool(){
JedisPoolConfig poolCfg = new JedisPoolConfig();
// 最大空闲数
poolCfg.setMaxIdle(50);
// 最大连接数
poolCfg.setMaxTotal(100);
// 最大等待毫秒数
poolCfg.setMaxWaitMillis(20000);
// 使用配置创建连接池
JedisPool pool = new JedisPool(poolCfg, "localhost"); // 从连接池中获取单个连接
Jedis jedis = pool.getResource();
// 如果需密码
// jedis.auth("password")
return pool; }
package com.ssm.chapter17.main;

import com.ssm.chapter17.jedis.JedisTest;

public class Chapter17Main {
public static void main(String[] args) {
testJedis();
}
private static void testJedis() {
JedisTest jedisTest = new JedisTest();
jedisTest.testJdedis();
}
}

17.3.2 在Spring中使用Redis

    使用Spring配置JedisPoolConfig对象

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大空闲数 -->
<property name="maxIdle" value="50"></property>
<!-- 最大连接数 -->
<property name="maxTotal" value="100"></property>
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="20000"></property>
</bean>

    配置JedisConnectionFactory

    <bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="localhost"></property>
<property name="port" value="6379"></property>
<!-- <property name="password" ref="password"></property> --> <property name="poolConfig" ref="poolConfig"></property>
</bean>

 配置Spring RedisTemplate

<bean id="jdkSerializationRedisSerializer"
class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean> <bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="keySerializer" ref="stringRedisSerializer"></property>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
</bean>

       使用Redis保存角色类对象

package com.ssm.chapter17.pojo;

import java.io.Serializable;
/**
* 注意,对象要可序列化,需要实现Serializable接口,往往要重写serialVersionUID
*
* @author zhongzh
*
*/
public class Role implements Serializable{ /**
*
*/
private static final long serialVersionUID = 6977402643848374753L; private long id; private String roleName; private String note; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getRoleName() {
return roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} public String getNote() {
return note;
} public void setNote(String note) {
this.note = note;
} }

        使用RedisTemplate保存Role对象

   private static void testSpring() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1L);
role.setRoleName("role_name_1");
role.setNote("role_note_1");
redisTemplate.opsForValue().set("role_1", role);
Role role1 = (Role) redisTemplate.opsForValue().get("role_1");
System.out.println(role.getRoleName());
}

使用SessionCallback接口

   private static void testSessionCallback(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
Role role = new Role();
role.setId(1);
role.setRoleName("role_name_1");
role.setNote("role_note_1");
SessionCallback<Role> sessionCallback = new SessionCallback<Role>() { public Role execute(RedisOperations ops)
throws DataAccessException {
// TODO Auto-generated method stub
ops.boundValueOps("role_1").set(role);
return (Role) ops.boundValueOps("role_1").get();
}
};
Role savedRole = (Role) redisTemplate.execute(sessionCallback);
System.out.println(savedRole.getId());
}

应改为:

private static void testSessionCallback(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
final Role role = new Role();
role.setId(1);
role.setRoleName("role_name_1");
role.setNote("role_note_1");
SessionCallback<Role> sessionCallback = new SessionCallback<Role>() { public Role execute(RedisOperations ops)
throws DataAccessException {
// TODO Auto-generated method stub
ops.boundValueOps("role_1").set(role);
return (Role) ops.boundValueOps("role_1").get();
}
};
Role savedRole = (Role) redisTemplate.execute(sessionCallback);
System.out.println(savedRole.getId());
}

最新文章

  1. NGINX 定时器
  2. PHP连接MySQL报错:SQLSTATE[HY000] [2002] Can&#39;t connect to local MySQL server through socket &#39;MySQL&#39; (2)
  3. 工程目录 Java/Web/Maven
  4. osg
  5. Android Material Design调色板
  6. SilverlightLoader使用托管代码创建自定义载入界面及动态加载XAP
  7. iOS8互动的新通知
  8. WEB消息推送-框架篇
  9. JVM 指令
  10. Docker最全教程之树莓派和Docker(十五)
  11. 【转】给word中的代码着色
  12. Linxu系统下JDK1.7(rpm)安装
  13. Linux学习-实验楼(1)
  14. English trip EM2-LP-4B At School Teacher:Russell
  15. Spark LogisticRegression 逻辑回归之建模
  16. Kibana5.x界面简要介绍(含x-pack插件)
  17. js 把字符串保存为txt文件,并下载到本地
  18. How to fix &quot;FAILURE DURING CONVERSION TO COFF: FILE INVALID OR CORRUPT&quot;
  19. sql解决避免除以零的错误
  20. Unity关于方法事件生命周期官方文档

热门文章

  1. python--5、包
  2. 日期对话框(DatePickerDialog)和时间对话框(TimePickerDialog)
  3. Java 入门作业
  4. 【PostgreSQL-9.6.3】psql常用命令
  5. 【SQL】CASE与DECODE
  6. 连接SQL Server数据库语法
  7. windows下安装Python-Whl文件
  8. php常用字符串和例子
  9. 【转】虚拟化(三):vsphere套件的安装注意及使用
  10. html表单练习