package com.jekin.example.service.impl;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.jekin.example.service.RedisService;
/**
* redis API
*
* @author fengjk
* @date 2017年10月13日
* @since 1.0
*/
@Service
@Transactional
public class RedisServiceImpl implements RedisService {
private static String redisCode = "utf-8";
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
*
*
* @author fengjk
* @param srckey
* @param dstkey
* @return
*/
@Override
public String rpoppush(final String srckey, final String dstkey) {
return redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
return new String(connection.rPopLPush(srckey.getBytes(), dstkey.getBytes()), redisCode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
});
}
/**
*
*
* @author fengjk
* @param key
* @param start
* @param end
* @return
*/
@Override
public List<String> lrange(final String key, final int start, final int end) {
return redisTemplate.execute(new RedisCallback<List<String>>() {
List<String> result = new ArrayList<String>();
public List<String> doInRedis(RedisConnection connection) throws DataAccessException {
List<byte[]> bytelist = connection.lRange(key.getBytes(), start, end);
for (byte[] b : bytelist) {
try {
result.add(new String(b, redisCode));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return result;
}
});
}
/**
*
*
* @author fengjk
* @param key
* @return
*/
@Override
public String lpop(final String key) {
return redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
byte[] result = connection.lPop(key.getBytes());
if (result != null) {
try {
return new String(result, redisCode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "";
}
});
}
/**
*
*
* @author fengjk
* @param key
* @param size
* @return
*/
@Override
public List<?> lpop(final String key, final long size) {
List<Object> list = redisTemplate.executePipelined(new RedisCallback<List<Object>>() {
public List<Object> doInRedis(RedisConnection connection) throws DataAccessException {
byte[] keyBytes = key.getBytes();
for (int i = 0; i < size; i++) {
connection.lPop(keyBytes);
}
return null;
}
});
list.removeAll(Collections.singleton(null));
return list;
}
/**
*
*
* @author fengjk
* @param key
* @param values
* @return
*/
@Override
public long rpush(final String key, final String... values) {
return redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
for (String v : values) {
result = connection.rPush(key.getBytes(), v.getBytes());
}
return result;
}
});
}
/**
*
*
* @author fengjk
* @param key
* @param values
* @return
*/
@Override
public long rpush(final String key, final Collection<String> values) {
return redisTemplate.opsForList().rightPushAll(key, values);
}
/**
*
*
* @author fengjk
* @param key
* @param values
* @return
*/
@Override
public int hmset(final String key, final Map<String, String> values) {
try {
redisTemplate.opsForHash().putAll(key, values);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return 0;
}
/**
*
*
* @author fengjk
* @param key
* @return
*/
@Override
public Map<Object, Object> hgetAll(final String key) {
return redisTemplate.boundHashOps(key).entries();
}
/**
*
*
* @author fengjk
* @param key
* @param hashKey
* @param value
* @return
*/
@Override
public int hput(final String key, final String hashKey, final String value) {
try {
redisTemplate.opsForHash().put(key, hashKey, value);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
return 0;
}
/**
*
*
* @author fengjk
* @param key
* @param field
* @return
*/
@Override
public String hget(final String key, final String field) {
Object obj = redisTemplate.opsForHash().get(key, field);
if (obj == null)
return null;
return String.valueOf(obj);
}
/**
*
*
* @author fengjk
* @param key
* @return
*/
@Override
public List<?> hkeys(final String key) {
Set<Object> sets = redisTemplate.opsForHash().keys(key);
List<Object> list = new ArrayList<Object>();
for (Object b : sets) {
list.add(String.valueOf(b));
}
return list;
/*
* return redisTemplate.execute(new RedisCallback<List<Object>>(){
* List<Object> list = new ArrayList<Object>(); public List<Object>
* doInRedis(RedisConnection connection) throws DataAccessException {
* Set<byte[]> sets=connection.hKeys(key.getBytes());
* if(!sets.isEmpty()){ for(byte[] b : sets){
* list.add(redisTemplate.getValueSerializer().deserialize(b).toString()
* ); try { list.add(new String (b , redisCode)); } catch
* (UnsupportedEncodingException e) { e.printStackTrace(); } } return
* list; } return null; } });
*/
}
/**
*
*
* @author fengjk
* @param key
* @param field
*/
@Override
public void hdel(final String key, final String field) {
redisTemplate.opsForHash().delete(key, field);
}
/**
*
*
* @author fengjk
* @param key
* @param liveTime
* @param values
* @return
*/
@Override
public long rpush(final String key, final int liveTime, final String... values) {
return redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
long result = 0;
for (String v : values) {
connection.rPush(key.getBytes(), v.getBytes());
}
if (liveTime > 0) {
connection.expire(key.getBytes(), liveTime);
}
return result;
}
});
}
/**
*
*
* @author fengjk
* @param key
* @return
*/
@Override
public String rpop(final String key) {
return redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
byte[] result = connection.rPop(key.getBytes());
if (result != null) {
try {
return new String(result, redisCode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "";
}
});
}
/**
*
*
* @author fengjk
* @param key
* @param index
* @param value
* @return
*/
@Override
public String lset(final String key, final long index, final String value) {
return redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) throws DataAccessException {
connection.lSet(key.getBytes(), index, value.getBytes());
return "success";
}