package com.caimei.www.service.redis.impl;


import com.caimei.www.service.redis.RedisService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author 王永煌
 * @date 2015年6月19日
 * @time 上午10:09:43
 * @version 1.0
 * @company Shenzhen Caimei Information Technology Co., LTD
 * @license Copyright © 2014 CAIMEI365.com All Rights Reserved.
 */
@Service
public class RedisServiceImpl implements RedisService {
	@Resource
	private RedisTemplate<Serializable, Object> redisTemplate;

	@Override
	public void remove(String... keys) {
		for(String key :keys){
			remove(key);
		}
	}

	@Override
	public void removePattern(String pattern) {
		Set<Serializable> keys = redisTemplate.keys(pattern);
		if (keys.size() > 0){
			redisTemplate.delete(keys);
		}
	}

	@Override
	public void remove(String key) {
		if (exists(key)) {
			redisTemplate.delete(key);
		}
	}

	@Override
	public boolean exists(String key) {
		return redisTemplate.hasKey(key);
	}

	@Override
	public Object get(String key) {
		Object result = null;
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		result = operations.get(key);
		return result;
	}

	@Override
	public boolean set(String key, Object value) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean set(String key, Object value, Long expireTime_seconds) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key,expireTime_seconds,TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public long rightPushForList(String key, Object value) {
		return redisTemplate.opsForList().rightPush(key, value);
	}

	@Override
	public long leftPushForList(String key, Object value) {
		return redisTemplate.opsForList().leftPush(key, value);
	}

	@Override
	public long listSize(String key) {
		return redisTemplate.opsForList().size(key);
	}

	@Override
	public List<?> getList(String Key, int begin, int end) {
		return redisTemplate.opsForList().range(Key, begin, end);
	}

	@Override
	public Object rightPopForList(String key) {
		return redisTemplate.opsForList().rightPop(key);
	}

	@Override
	public Object leftPopForList(String key) {
		return redisTemplate.opsForList().leftPop(key);
	}

	@Override
	public void removeList(String Key, long index, Object value) {
		redisTemplate.opsForList().remove(Key, index, value);
	}

	@Override
	public void setList(String Key, long index, Object value) {
		redisTemplate.opsForList().set(Key, index, value);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void setList(String key, List list) {
		if(list!=null&&list.size()>0){
			for (Object object : list) {
				rightPushForList(key,object);
			}
		}
	}

	@Override
	public void setMap(String key, Map<String, Object> map) {
		redisTemplate.opsForHash().putAll(key, map);
	}

	@Override
	public Object get(String key, String mapKey) {
		return redisTemplate.opsForHash().get(key, mapKey);
	}

	@Override
	public void setMapByKV(String key, Object hashKey, Object hashValue) {
		redisTemplate.opsForHash().put(key, hashKey,hashValue);
	}

	@Override
	public void removeHash(String key, String hashKey) {
		redisTemplate.opsForHash().delete(key, hashKey);
	}

	@Override
	public Map<Object, Object> getEntries(String key) {
		return redisTemplate.opsForHash().entries(key);
	}

	@Override
	public long increase(String key, long step) {
		return redisTemplate.opsForValue().increment(key, step);
	}

	@Override
	public long getExpireTime(String key) {
		return redisTemplate.getExpire(key, TimeUnit.SECONDS);
	}

}
