命令参考手册 http://redisdoc.com/
Redis 支持5种数据类型, 是指Value的类型
Hash 类型
List 类型
Jdeis 是 Redis 官方推荐的Java Redis API
其主页地址为: https://github.com/xetorthio/jedis
使用步骤:
导入Jedis API
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
编写测试案例:
@Test
public void testJedis(){
Jedis jedis = new Jedis("10.7.11.19");
jedis.set("fan", "传奇");
jedis.setex("login", 15, "Andy");
String str = jedis.get("fan");
System.out.println(str);
jedis.close();
}
Jedis 对象提供了访问Redis数据库的全部方法. Redis有哪些操作命令, Jedis就提供了哪些操作方法.
Spring 提供了Redis访问的支持, 其底层也使用了Jedis API.
Spring Data Redis 提供了面向对象的访问接口方法, 可以将对象直接序列化保存到Redis数据库中.
使用步骤:
导入API
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
配置spring容器, 初始化RedisTempalte对象: spring-redis.xml
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"/>
<property name="hostName"
value="10.7.11.19"></property>
</bean>
<!-- redis template definition -->
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory"
ref="jedisConnFactory"/>
</bean>
使用RedisTemplate访问Redis
ClassPathXmlApplicationContext ctx;
RedisTemplate<String, Object> tpl;
@Before
public void init(){
ctx=new ClassPathXmlApplicationContext(
"spring-redis.xml");
tpl=ctx.getBean("redisTemplate",
RedisTemplate.class);
}
@After
public void destory(){
ctx.close();
}
@Test
public void testRedisTemplate(){
tpl.opsForValue().set("man", "Java Man");
String str = (String)
tpl.opsForValue().get("man");
System.out.println(str);
System.out.println(tpl);
}
@Test
public void testSaveObject(){
User user = new User("范传奇", 12);
tpl.opsForValue().set("starMan",
user, 15, TimeUnit.SECONDS);
User guy=(User)tpl.opsForValue()
.get("starMan");
System.out.println(guy);
}
Redis在互联网架构中经常充当数据库的缓冲作用, 将经常读取的数据缓冲到Redis中, 可以提高数据读取的效率, 实现"高性能".
学子商城是互联网项目, 性能直接关系到用户体验.
为了解决学子商城的高性能问题, 为学子商城增加了Redis缓存, 提高数据的读取性能.
重构步骤:
导入Spring Data Redis
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.2.RELEASE</version>
</dependency>
注意:Spring容器版本提升到4.3.8.RELEASE, 这个版本与Spring-data-Redis: 1.8.2.RELEASE是兼容的.
添加spring-redis.xml 配置文件
<bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true" />
<property name="hostName" value="10.7.11.19"></property>
</bean>
<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory" />
</bean>
更新业务层, 增加缓存功能 DictService
@Resource
private RedisTemplate<String, Object>
redisTemplate;
public synchronized List<Province> getProvince() {
//先查询Redis中是否有省份信息
List<Province> list= (List<Province>)
redisTemplate.opsForValue()
.get("province");
if(list==null){
System.out.println("查询Provice");
list=dictMapper.selectProvince();
redisTemplate.opsForValue()
.set("province",list,1,TimeUnit.DAYS);
}
return list;
}
public synchronized List<City> getCity(String provinceCode) {
//查询Redis
List<City> cities = (List<City>)
redisTemplate.opsForValue()
.get("provice"+provinceCode);
if(cities==null){
cities=dictMapper.selectCity(provinceCode);
redisTemplate.opsForValue()
.set("provice"+provinceCode, cities,
1, TimeUnit.DAYS);
}
return cities;
}
测试...
Nginx集群架构中可以利用ip_hash解决session问题, 也可以利用 Redis 共享Session, 来解决Session问题.
已经有现成的Tomcat Redis session共享解决方案: https://github.com/bluejeansnet/tomcat-redis-session-manager
Tomcat 本身就是开放架构, 可以进行Session管理器的替换. Tomcat Redis session共享解决方案, 就是替换了Tomcat的Session管理器.
使用步骤:
配置Tomcat的conf/context.xml, 替换Session管理器
<Valve className="com.bluejeans.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.bluejeans.tomcat.redissessions.RedisSessionManager"
host="10.7.11.19"
port="6379"
database="0"
maxInactiveInterval="60"
sessionPersistPolicies="SAVE_ON_CHANGE"
/>
在 webapps/ROOT 中添加测试文件
add.jsp
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<body>
<h1>218 Save Session</h1>
<%
session.setAttribute("message", "Hello World!");
%>
</body>
</html>
get.jsp
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<body>
<h1>218 Get Session</h1>
<%
String str=(String)session.getAttribute("message");
%>
<%=str%>
</body>
</html>
集群中每个Tomcat都进行如上配置
通过域名访问集群 add.jsp 和 get.jsp 可以探测到Session被共享了.
也就是 Subversion 是主流的版本管理工具, 用于项目团队的协作.
网站: http://subclipse.tigris.org/
Eclipse 需要 添加 subclipse 插件才能与SVN服务器进行通信。