PushRedisService.java 4.71 KB
package com.yoho.yhmessage.redis;

import com.yoho.core.redis.cluster.annotation.Redis;
import com.yoho.core.redis.cluster.operations.nosync.*;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 
 * PUSH专用redis,和公用redis集群分开
 * 
 * @author hugufei
 */

@Service
public class PushRedisService {

	@Redis("yohoPushRedis")
	private YHRedisTemplate pushRedisTemplate;

	@Redis("yohoPushRedis")
	private YHValueOperations pushValueOperations;

	@Redis("yohoPushRedis")
	private YHListOperations pushListOperations;

	@Redis("yohoPushRedis")
	private YHSetOperations pushSetOperations;

	@Redis("yohoPushRedis")
	private YHHashOperations pushHashOperations;

	RedisSerializer<String> stringSerializer = new StringRedisSerializer();

	/*************************** redisTemplate *****************************/
	public YHRedisTemplate getPushRedisTemplate() {
		return pushRedisTemplate;
	}

	public void longExpire(RedisKeyBuilder redisKey, long expiredTs, TimeUnit timeUnit) {
		RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance().appendVar(redisKey);
		pushRedisTemplate.expire(redisKeyBuilder, expiredTs, timeUnit);
	}

	public void delete(RedisKeyBuilder redisKey) {
		pushRedisTemplate.delete(redisKey);
	}

	public long getExpire(RedisKeyBuilder redisKey) {
		RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance().appendVar(redisKey);
		return pushRedisTemplate.getExpire(redisKeyBuilder);
	}

	/*************************** value_operation *****************************/
	public boolean value_setIfAbsent(RedisKeyBuilder redisKey, String string) {
		return this.pushValueOperations.setIfAbsent(redisKey, string);
	}

	/*************************** list_operation *****************************/
	public void list_rightPushAll(RedisKeyBuilder redisKeyBuilder, List<String> values) {
		this.pushListOperations.rightPushAll(redisKeyBuilder, values);
	}

	public List<String> list_range(RedisKeyBuilder redisKey, long fromIndex, long toIndex) {
		return this.pushListOperations.range(redisKey, fromIndex, toIndex);
	}

	public long list_size(RedisKeyBuilder redisKey) {
		return this.pushListOperations.size(redisKey);
	}

	public void setValue(RedisKeyBuilder redisKey, String value, long expiredTs, TimeUnit timeUnit) {
		pushValueOperations.set(redisKey, value, expiredTs, timeUnit);
	}

	public String getValue(RedisKeyBuilder redisKey) {
		return pushValueOperations.get(redisKey);
	}

	public boolean isMember(RedisKeyBuilder redisKey, String value) {
		return pushSetOperations.isMember(redisKey, value);
	}

	public void sAdd(RedisKeyBuilder redisKey, String value) {
		pushSetOperations.add(redisKey, value);
	}

	public long increment(RedisKeyBuilder redisKey, long increment) {
		return pushValueOperations.increment(redisKey, increment);
	}

	public long hashIncrement(RedisKeyBuilder redisKey, String hKey, long increment, int expireHours){
		long count = pushHashOperations.increment(redisKey,hKey,increment);
		//设置超时时间
		if (pushRedisTemplate.getExpire(redisKey) <= 0) {
			pushRedisTemplate.longExpire(redisKey, expireHours, TimeUnit.HOURS);
		}
		return count;
	}

	public void getSetFilter(Set<String> orginSet, String redisKey){
		if(CollectionUtils.isEmpty(orginSet)){
			return;
		}
		List<String> orginList = new ArrayList<>(orginSet);
		Set<String> invalidSet = new HashSet<>();
		List<Object> filteredList = pushRedisTemplate.executePipelined(connection -> {
			connection.openPipeline();
			byte[] key = stringSerializer.serialize(redisKey);
			for (String str : orginList) {
				connection.sIsMember(key, stringSerializer.serialize(str));
			}
			return null;
		});
		for (int j = 0; j < orginSet.size(); j++) {
			//如果存在 则过滤
			if ("true".equals(filteredList.get(j).toString())) {
				invalidSet.add(orginList.get(j));
			}
		}
		orginSet.removeAll(invalidSet);
	}

	public void setListValue(String redisKey,List<String> valueList,int hours){
		//写入redis中的当天发送uid set集合
		pushRedisTemplate.executePipelined(connection -> {
			connection.openPipeline();
			byte[] key = stringSerializer.serialize(redisKey);
			for (String value : valueList) {
				connection.sAdd(key, stringSerializer.serialize(value));
			}
			return null;
		});
		//设置超时时间
		RedisKeyBuilder redisTodayKey = RedisKeyBuilder.newInstance().appendFixed(redisKey);
		pushRedisTemplate.expire(redisTodayKey,hours,TimeUnit.HOURS);
	}

}