All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.leopard.data.kit.rank.UniqueCountRankTimeBucketImpl Maven / Gradle / Ivy

Go to download

常见数据源组合操作组件,如:新数量计数器(日、周、月等)、按自然时间的数量排名等。

The newest version!
package io.leopard.data.kit.rank;

import java.util.Date;

public class UniqueCountRankTimeBucketImpl extends CountRankTimeBucketImpl implements UniqueCountRank {

	protected String getUniqueKey() {
		return this.key + ":unique";
	}

	protected String getUniqueId(String member, String id) {
		return member + ":" + id;
	}

	@Override
	public long incr(String member, String id, long count, Date posttime) {
		String key = getUniqueKey();
		String uniqueId = this.getUniqueId(member, id);

		Double time = redis.zscore(key, uniqueId);
		System.out.println("time:" + time + " id:" + id);
		if (isCurrentTimeBucket(time)) {
			// 当前时间段已经操作过
			return 0;
		}
		long result = this.incr(member, count, posttime);

		redis.zadd(key, System.currentTimeMillis(), uniqueId);
		return result;
	}

	protected long getTimeBucketMillis() {
		if (timeBucket == TimeBucket.DAY) {
			return 1000L * 60 * 60 * 24;
		}
		else {
			throw new IllegalArgumentException("未知时间段[" + timeBucket + "].");
		}
	}

	protected boolean isCurrentTimeBucket(Double time) {
		if (time == null) {
			return false;
		}

		long timeBucketMillis = this.getTimeBucketMillis();
		long millls = System.currentTimeMillis() - time.longValue();
		return millls < timeBucketMillis;
	}

	@Override
	public boolean delete(String member, String id) {
		String key = getUniqueKey();
		String uniqueId = this.getUniqueId(member, id);
		Double time = redis.zscore(key, uniqueId);
		if (!isCurrentTimeBucket(time)) {
			// 当前时间段已经操作过
			return false;
		}
		Long num = redis.zrem(key, uniqueId);
		return num != null && num > 0;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy