第一步:下载:http://www.redis.io/download,然后上网查资料http://os.51cto.com/art/201403/431103.htm安装好
下面是我下载的地址https://github.com/ServiceStack/redis-windows#current-version-30501-january-25-2016
然后找到Download the redis-latest.zip native 64bit Windows port of redis,点击把zip下载下来解压好,然后cmd,命令为:C:\Users\lenovo>d:
D:\>cd \eclipse\redis
D:\eclipse\redis>redis-server.exe redis.windows.conf
记得不要把命令窗口关了咯,否则呆会测试就没法测了
第二步:依赖:
<!--redis客户端:jedis -->
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<!-- protostuff序列化依赖 自定义依赖需要的jar包-->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
第三步:window->show view->problems->查看下导入的依赖版本是否有问题->修改适合自己项目的版本。
package cn.itcast.jk.dao.impl;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import cn.itcast.jk.domain.Factory;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
/**
* redis缓存dao
* 注意:factory类一定要是pojo类(有get和set方法的那种)
*
*/
public class RedisDao {
private final JedisPool jedisPool;
//声明schema,用于自定义序列化
private RuntimeSchema<Factory> schema=RuntimeSchema.createFrom(Factory.class);
//构造器
public RedisDao(String ip,int port){
jedisPool=new JedisPool(ip, port);
}
/**
* 过程:byte[]->反序列化->Object(Factory)
*/
public Factory getFactory(String id){
//redis操作逻辑
try {
Jedis jedis=jedisPool.getResource();
try{
String key="factory:"+id;
byte[] bytes=jedis.get(key.getBytes());
if(bytes!=null){//不等于null说明缓存获取到了
//空对象
Factory factory=schema.newMessage();
//factory 被反序列化
ProtostuffIOUtil.mergeFrom(bytes,factory,schema);
return factory;
}
}finally{
jedis.close();
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
/**
* 过程:Object(Factory)->系列化->byte[]
*/
public String setFactory(Factory factory){
try {
Jedis jedis=jedisPool.getResource();
try{
//获取一个key
String key="factory:"+factory.getId();
//第三个参数LinkedBuffer为缓存器,给的是默认的大小
byte[] bytes=ProtostuffIOUtil.toByteArray(factory, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE));
//超时缓存
int timeout=60*60;//缓存一小时
String result=jedis.setex(key.getBytes(), timeout, bytes);
return result;
}finally{
jedis.close();
}
} catch (Exception e) {
System.out.println(e);
}
return null;
}
}
第五步:写bean,把redisdao写到spring配置文件里
<bean id="redisDao" class="cn.itcast.jk.dao.impl.RedisDao">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="6379"/>
</bean>
package test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.itcast.jk.dao.FactoryDao;
import cn.itcast.jk.dao.impl.RedisDao;
import cn.itcast.jk.domain.Factory;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:beans.xml"})
public class ExtCproductDaoImplTest {
@Resource
private RedisDao redisDao;
@Resource
private FactoryDao factoryDao;
@Test
public void testRedis(){
String id="factoryId";
Factory factory=redisDao.getFactory(id);
if(factory==null){
factory=this.factoryDao.getById(id);
if(factory!=null){
String result=redisDao.setFactory(factory);
System.out.println(result);
factory=redisDao.getFactory(id);
System.out.println(factory);
}
}
}
}
第六步:server
package cn.itcast.jk.service.impl;
import java.io.Serializable;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import cn.itcast.jk.dao.FactoryDao;
import cn.itcast.jk.dao.impl.RedisDao;
import cn.itcast.jk.domain.Factory;
import cn.itcast.jk.service.FactoryService;
@Service
public class FactoryServiceImpl implements FactoryService{
@Resource
private FactoryDao factoryDao;
@Resource
private RedisDao redisDao;
public Factory getById(Serializable id) {
//缓存优化
//1.访问redis
Factory factory =this.redisDao.getFactory((String)id);
if(factory==null){
//2.访问数据库
factory= this.factoryDao.getById(id);
if(factory==null){
System.out.println("对象不存在");
}else{
//3.放入redis
this.redisDao.setFactory(factory);
}
}
return factory;
}
}