Redis(2)用jedis实现在java中使用redis
昨天已经在 windows 环境下安装使用了 redis。
下面准备在 java 项目中测试使用 redis。
redis 官网推荐使用 jedis 来访问 redis。所以首先准备了 jedis 的 jar 包,以及需要依赖的 jar 包。
commons-pool2-2.3
hamcrest-core-1.3
jedis-2.7.2.jar
因为 redis 也是属于一种数据库,也是对数据的访问,所以把他放置在 dao 层,与 service 分开
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool;import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.xhxkj.ssm.entity.UserEntity;/**
- 访问 redis 数据层
- @author XX
*/
public class RedisDao {</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">final</span> JedisPool jedisPool;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">redis连接池</span> <span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 构造方法 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> ip 访问的ip * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> port 访问的端口 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span> RedisDao(String ip, <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> port) { jedisPool </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> JedisPool(ip,port); } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个schema用来序列化</span> <span style="color: rgba(0, 0, 255, 1)">private</span> RuntimeSchema<UserEntity> schema = RuntimeSchema.createFrom(UserEntity.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 通过用户名获取redis中对应的用户信息 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> username 输入的用户名 * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 存在返回:这个对象,不存在返回:null </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> UserEntity getUser(String username) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">redis操作</span> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{ Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在redis中存放时,key的书写规则,官方推荐,对象:对象属性 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">也就是获取时利用“user:username”作为键来得到值</span> String key = "user:" +<span style="color: rgba(0, 0, 0, 1)"> username; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自定义序列化 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在redis中获取的值一定是一个字节数组,需要通过反序列化转换成java对象</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes =<span style="color: rgba(0, 0, 0, 1)"> jedis.get(key.getBytes()); </span><span style="color: rgba(0, 0, 255, 1)">if</span>(bytes != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取一个空对象</span> UserEntity user =<span style="color: rgba(0, 0, 0, 1)"> schema.newMessage(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">反序列化后放置在user中</span>
ProtostuffIOUtil.mergeFrom(bytes, user, schema);
return user;
}
}finally{
jedis.close();
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}</span><span style="color: rgba(0, 128, 0, 1)">/**</span><span style="color: rgba(0, 128, 0, 1)"> * 在缓存中存放user对象 * </span><span style="color: rgba(128, 128, 128, 1)">@param</span><span style="color: rgba(0, 128, 0, 1)"> user * </span><span style="color: rgba(128, 128, 128, 1)">@return</span><span style="color: rgba(0, 128, 0, 1)"> 成功返回“OK”;失败返回错误信息 </span><span style="color: rgba(0, 128, 0, 1)">*/</span> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String putUser(UserEntity user) { </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { Jedis jedis </span>=<span style="color: rgba(0, 0, 0, 1)"> jedisPool.getResource(); </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过对应的键存放user对象</span> String key = "user:" +<span style="color: rgba(0, 0, 0, 1)"> user.getUsername(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自定义序列化操作,利用protostuff将对象序列化成字节数组</span> <span style="color: rgba(0, 0, 255, 1)">byte</span>[] bytes =<span style="color: rgba(0, 0, 0, 1)"> ProtostuffIOUtil.toByteArray(user, schema, LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE)); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">缓存时间1小时,缓存的时间是用秒来计的</span> <span style="color: rgba(0, 0, 255, 1)">int</span> timeout = 60*60<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在redis中存放这个对象</span> <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> jedis.setex(key.getBytes(),timeout,bytes); } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">{ jedis.close(); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; }
}
这里提供了两个方法,分别是 put 和 get,其中需要用到序列化和反序列化的操作,用到的 jar 包有下面这几个
protostuff-core-1.0.8.jar
protostuff-runtime-1.0.8.jar
protostuff-collectionschema-1.0.8.jar
protostuff-api-1.0.8.jar
protostuff 属于性能相当优秀的一种
在 spring 中配置
<!-- redisDao -->
<bean id="redisDao" class="com.xxx.dao.redis.RedisDao">
<constructor-arg index="0" value="localhost"/>
<constructor-arg index="1" value="6379"/>
</bean>
之后就可以直接在服务层调用 redisDao 的方法了
// 首先去 redis 中寻找是否存在缓存的用户信息
UserEntity resultUser = redisDao.getUser("xx");
// 如果不存在,那就在缓存中放置一个用户信息
if(resultUser == null)
{
String result = redisDao.putUser(user);
System.out.println(result);
return null;
}
else
{
return resultUser;
}
需要注意的是,如果存放成功,返回是一个字符串“OK”
在使用前一定要确定你的 redis 服务处于开启状态,cmd 窗口是开着的
因为网络上面很多都是用 maven 配置的,所以 jar 包比较难找,jar 包间的依赖确实比较麻烦,推荐一个网站http://maven.outofmemory.cn/
这个网站能很好的找到各种 jar 包,并且告诉你依赖的关系,非常方便