<
dependency
>
<
groupId
>
redis.clients
groupId
>
<
artifactId
>
jedis
artifactId
>
<
version
>
2.9.0
version
>
dependency
>
<
dependency
>
<
groupId
>
com.dyuproject.protostuff
groupId
>
<
artifactId
>
protostuff-core
artifactId
>
<
version
>
1.1.1
version
>
dependency
>
<
dependency
>
<
groupId
>
com.dyuproject.protostuff
groupId
>
<
artifactId
>
protostuff-runtime
artifactId
>
<
version
>
1.1.1
version
>
dependency
>
|
package
hxf.utils;
import
com.dyuproject.protostuff.LinkedBuffer;
import
com.dyuproject.protostuff.ProtobufIOUtil;
import
com.dyuproject.protostuff.ProtostuffIOUtil;
import
com.dyuproject.protostuff.Schema;
import
com.dyuproject.protostuff.runtime.RuntimeSchema;
/**
* 序列化工具类
*/
public class
ProtostuffUtil {
public
ProtostuffUtil() {
}
/**
* 对象序列化
*
@param
o
需要序列化对象
*
@param
*
@return
*/
public static
<
T
>
byte
[] serializer(
T
o) {
Schema schema = RuntimeSchema.
getSchema
(o.getClass());
//通过对象的类构建对应的schema
return
ProtobufIOUtil.
toByteArray
(o, schema, LinkedBuffer.
allocate
(LinkedBuffer.
DEFAULT_BUFFER_SIZE
));
//保存数据
}
/**
* 对象反序列化
*
@param
bytes
对象字节数组
*
@param
clazz
Class对象
*
@param
*
@return
*/
public static
<
T
>
T
deserializer(
byte
[] bytes, Class<
T
> clazz) {
T
obj =
null
;
try
{
Schema schema = RuntimeSchema.
getSchema
(clazz);
//通过对象的类构建对应的schema;
obj = (
T
) schema.newMessage();
//通过schema新建一个对象,这里需要转换一下
ProtostuffIOUtil.
mergeFrom
(bytes, obj, schema);
//数据反序列化
}
catch
(Exception e) {
e.printStackTrace();
}
return
obj;
}
}
|
package
hxf.redis.dao.impl;
import
hxf.entity.Seckill;
import
hxf.redis.dao.RedisDataSource;
import
hxf.utils.ProtostuffUtil;
import
org.slf4j.Logger;
import
org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.annotation.
Autowired
;
import
org.springframework.stereotype.
Repository
;
import
redis.clients.jedis.ShardedJedis;
@Repository
public class
RedisClientTemplate {
private
Logger
logger
= LoggerFactory.
getLogger
(
this
.getClass());
@Autowired
private
RedisDataSource
redisDataSource
;
public void
disconnect() {
ShardedJedis shardedJedis =
redisDataSource
.getRedisClient();
shardedJedis.disconnect();
}
private static final
Logger
log
= LoggerFactory.
getLogger
(RedisClientTemplate.
class
);
/**
* 添加一个对象
*
@param
id
主键id
*
@param
o
对象数据
*
@param
*
@return
成功返回true,否则返回false
*/
public
<
T
>
boolean
setObj(String id,
T
o) {
boolean
result =
false
;
ShardedJedis shardedJedis =
redisDataSource
.getRedisClient();
if
(shardedJedis ==
null
) {
return
result;
}
try
{
String result1 = shardedJedis.set(id.getBytes(), ProtostuffUtil.
serializer
(o));
logger
.info(
"添加结果:"
+result1);
if
(result1.equals(
"ok"
)){
result =
true
;
}
}
catch
(Exception e) {
log
.error(e.getMessage(), e);
}
finally
{
redisDataSource
.close(shardedJedis,!result);
}
return
result;
}
public
<
T
>
T
getObj(String id, Class<
T
> clazz) {
T
result =
null
;
ShardedJedis shardedJedis =
redisDataSource
.getRedisClient();
if
(shardedJedis ==
null
) {
return
result;
}
try
{
byte
[] bytes = shardedJedis.get(id.getBytes());
if
(bytes !=
null
){
result = ProtostuffUtil.
deserializer
(bytes,clazz);
}
logger
.info(bytes.toString());
}
catch
(Exception e) {
log
.error(e.getMessage(), e);
}
finally
{
redisDataSource
.close(shardedJedis);
}
return
result;
}}
|
package
hxf.redis.dao.impl;
import
hxf.entity.Seckill;
import
junit.framework.TestCase;
import
org.junit.
Test
;
import
org.springframework.beans.factory.annotation.
Autowired
;
import
org.junit.runner.
RunWith
;
import
org.springframework.test.context.
ContextConfiguration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith
(SpringJUnit4ClassRunner.
class
)
//配置Spring文件
@ContextConfiguration
({
"classpath:spring/spring-redis.xml"
})
public class
RedisClientTemplateTest
extends
TestCase {
@Autowired
private
RedisClientTemplate
redisClientTemplate
;
@Test
public void
test()
throws
Exception {
Seckill seckill =
new
Seckill();
seckill.setSeckillId(
1002
);
seckill.setName(
"100元秒杀苹果8"
);
seckill.setNumber(
20
);
redisClientTemplate
.setObj(seckill.getSeckillId()+
""
,seckill);
}
@Test
public void
testGet()
throws
Exception {
System.
out
.println(
redisClientTemplate
.getObj(
"1002"
,Seckill.
class
));
}
}
|