save方法,序列化是自己实现的:
public void save(final K redisKey,final V data){
if(redisKey != null && data != null){
redisTemplate.execute(new RedisCallback<V>() {
public V doInRedis(RedisConnection connection) throws DataAccessException {
byte[] bkey = ObjectTranscoder.serialize(redisKey);
connection.set(bkey,ObjectTranscoder.serialize(data));
connection.close();
return null;
}
});
}
}
incrBy方法:
public void incr(final K redisKey,final long increValue){
if(redisKey != null){
redisTemplate.execute(new RedisCallback<V>() {
public V doInRedis(RedisConnection connection) throws DataAccessException {
byte[] bkey = ObjectTranscoder.serialize(redisKey);
connection.incrBy(bkey, increValue);
connection.close();
return null;
}
});
}
}
public void save(final K redisKey,final V data){
if(redisKey != null && data != null){
redisTemplate.execute(new RedisCallback<V>() {
public V doInRedis(RedisConnection connection) throws DataAccessException {
byte[] bkey = ObjectTranscoder.serialize(redisKey);
connection.set(bkey,ObjectTranscoder.serialize(data));
connection.close();
return null;
}
});
}
}
incrBy方法:
public void incr(final K redisKey,final long increValue){
if(redisKey != null){
redisTemplate.execute(new RedisCallback<V>() {
public V doInRedis(RedisConnection connection) throws DataAccessException {
byte[] bkey = ObjectTranscoder.serialize(redisKey);
connection.incrBy(bkey, increValue);
connection.close();
return null;
}
});
}
}
public void incr(final K redisKey,final long increValue){
if(redisKey != null){
redisTemplate.execute(new RedisCallback<V>() {
public V doInRedis(RedisConnection connection) throws DataAccessException {
byte[] bkey = ObjectTranscoder.serialize(redisKey);
Object obj = ObjectTranscoder.deserialize(connection.get(bkey));
if(obj == null){
connection.close();
return null;
}
long value = Long.valueOf(obj.toString()) + increValue;
long expitedTime = connection.ttl(bkey);
connection.setEx(bkey,expitedTime,ObjectTranscoder.serialize(value));
connection.close();
return null;
}
});
}
}谢谢。
con.incr("a:a".getBytes());
这样试试看