package com.codingapi.tm.redis.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.codingapi.tm.config.ConfigReader; import com.codingapi.tm.netty.model.TxGroup; import com.codingapi.tm.redis.service.RedisServerService; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; /** * create by lorne on 2017/11/11 */ @Service public class RedisServerServiceImpl implements RedisServerService{ @Autowired private RedisTemplate redisTemplate; @Autowired private ConfigReader configReader; private Logger logger = LoggerFactory.getLogger(RedisServerServiceImpl.class); public String loadNotifyJson() { Set keys = redisTemplate.keys(configReader.getKeyPrefixCompensate()+"*"); ValueOperations value = redisTemplate.opsForValue(); JSONArray jsonArray = new JSONArray(); for(String key:keys){ String json = value.get(key); JSONObject jsonObject = new JSONObject(); jsonObject.put("key",key); jsonObject.put("value",JSONObject.parse(json)); jsonArray.add(jsonObject); } return jsonArray.toJSONString(); } @Override public void saveTransaction(String key, String json) { logger.info("redis save key:"+key+", val:"+json); ValueOperations value = redisTemplate.opsForValue(); value.set(key, json, configReader.getRedisSaveMaxTime(), TimeUnit.SECONDS); } @Override public TxGroup getTxGroupByKey(String key) { logger.info("redis get key:"+key); ValueOperations value = redisTemplate.opsForValue(); String json = value.get(key); if (StringUtils.isEmpty(json)) { return null; } return TxGroup.parser(json); } @Override public void saveCompensateMsg(String name, String json) { ValueOperations value = redisTemplate.opsForValue(); value.set(name, json); } @Override public List getKeys(String key) { Set keys = redisTemplate.keys(key); List list = new ArrayList(); for (String k : keys) { list.add(k); } return list; } @Override public List getValuesByKeys(List keys) { ValueOperations value = redisTemplate.opsForValue(); List list = new ArrayList<>(); for (String key : keys) { String json = value.get(key); list.add(json); } return list; } @Override public String getValueByKey(String key) { ValueOperations value = redisTemplate.opsForValue(); return value.get(key); } @Override public void deleteKey(String key) { redisTemplate.delete(key); } @Override public void saveLoadBalance(String groupName, String key, String data) { HashOperations value = redisTemplate.opsForHash(); value.put(groupName,key,data); } @Override public String getLoadBalance(String groupName, String key) { HashOperations value = redisTemplate.opsForHash(); return value.get(groupName,key); } }