第17章 Redis概述
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());
}
第17章 Redis概述的更多相关文章
- 第17章 中介者模式(Mediator Pattern)
原文 第17章 中介者模式(Mediator Pattern) 中介者模式 概述: 在软件开发中,我们有时会碰上许多对象互相联系互相交互的情况,对象之间存在复杂的引用关系,当需求更改时,对系统进 ...
- SSM框架学习笔记_第1章_SpringIOC概述
第1章 SpringIOC概述 Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. 1.1 控制反转IOC IOC(inversion of controller)是一种概念 ...
- 第一章 数据库概述、MySQL的安装和配置
第一章 数据库概述.MySQL的安装和配置 1.为什么要使用数据库 最早是纸质文件来存储数据 缺点:不易保存,占用空间大 计算机出现以后,采用软件来进行保存(excel) 缺点:容易损坏 文件 ...
- Laxcus大数据管理系统2.0(2)- 第一章 基础概述 1.1 基于现状的一些思考
第一章 基础概述 1.1 基于现状的一些思考 在过去十几年里,随着互联网产业的普及和高速发展,各种格式的互联网数据也呈现爆炸性增长之势.与此同时,在数据应用的另一个重要领域:商业和科学计算,在各种新兴 ...
- CSS3秘笈第三版涵盖HTML5学习笔记13~17章
第13章,构建基于浮动的布局 使用的是float(浮动)属性 注:float:none值将取消所有浮动,通常只用来取消元素中已经应用的浮动. 切记:不需要给正文的div设计宽度,即使设计成固定宽度也不 ...
- 《深入Java虚拟机学习笔记》- 第17章 异常
<深入Java虚拟机学习笔记>- 第17章 异常
- JavaScript高级程序设计(第三版)学习笔记11、12、17章
章, DOM扩展 选择符 API Selector API Level1核心方法querySelector .querySelectorAll,兼容的浏览器可以使用 Document,Element ...
- C++ Primer Plus 文章17章 进,输出和文件
文章17章 进.输出和文件 1.当到达输入句子.他将刷新输出缓冲区满输出电流 2.streambuf分类 它提供了用于各种操作的一个缓冲 ios_base类表示流的一般特征 ios基础的类ios_ba ...
- 【RL-TCPnet网络教程】第17章 RL-TCPnet之UDP通信
第17章 RL-TCPnet之UDP通信 本章节为大家讲解RL-TCPnet的UDP通信实现,学习本章节前,务必要优先学习第16章UDP用户数据报协议基础知识.有了这些基础知识之后,再搞本章 ...
随机推荐
- 【sqli-labs】 less21 Cookie Injection- Error Based- complex - string ( 基于错误的复杂的字符型Cookie注入)
这个和less20是一样的,唯一的不同在于添加了括号和使用了base64对cookie进行了编码(因为使用了base64_decode解码函数) admin被编码成了YWRtaW4=但是执行的SQL语 ...
- Newtonsoft.Json 处理日期格式
Newtonsoft.Json.Converters.IsoDateTimeConverter iso = new Newtonsoft.Json.Converters.IsoDateTimeConv ...
- luogu P4719 【模板】动态 DP 矩阵乘法 + LCT
方法二:LCT+矩阵乘法 上文中,我们用线段树来维护重链上的各种矩阵转移. 第二种方法是将树链剖分替换为动态树. 我们知道,矩阵乘法 $\begin{bmatrix} F_{u,0} & F_ ...
- Python基础(二)数据类型
(一)数字 Python3中的数字类型分为3种,分别是整型,浮点型以及复数. Python2种的数字类型分为4种,分别是整型,长整型,浮点型以及复数. 其中长整型时Python2为应对位数较大的而设置 ...
- python 生成HTmL报告页面
计划做一个html页面 py3.4 代码: # -*- coding=utf-8 -*- # import time,os class Template_mixin(object): "&q ...
- eas之日期选择控件
初始化打印控件KDPrinter ctrlPrinter = new KDPrinter(); 增加列 // 指定插入位置table.addColumn(index);// 插入到最后table.ad ...
- 15.5.6 【Task实现细节】跟踪栈
谈到栈帧(stack frame)时,可能会想到在方法中声明的局部变量.当然,可能还会注意到 一些隐藏的局部变量,如 foreach 循环中的迭代器.但栈上的内容不止这些,至少逻辑上是这样 . 很多 ...
- [POJ2404]Jogging Trails
我太弱了. 我们可以知道一个结论就是对于一个图的话假如所有点的度数都是偶数,那么只需要走一波欧拉回路. 所以我们就把奇点补成偶点. 将两个奇点补充到偶点的最佳方法是选择任意两个奇点连最短路径为权的边 ...
- SPU、SKU、ARPU是什么,我来记录一下我的理解
在电商系统里经常会提到“商品”.“单品”.“SPU”.“SKU”这几个词,那么这几个词到底是什么意思呢? 既然不知道是什么,那么我们就查一下:SPU = Standard Product Unit ( ...
- 2.Eclipse中绑定Tomcat
1.下载并成功安装Eclipse和Tomcat(该步骤有问题请查看我的相关经验): 2.打开Eclipse,单击“window”菜单,选择下方的“Preferences”: 3.找到Server下方的 ...