<commit_before>package com.linuxea;

import java.util.Map;

import com.google.common.collect.Maps;

/**
 *@date      2017年8月7日 上午9:09:06
 *@desc      map实现缓存
 *@version   1.0
 *@author    Linuxea.lin
*/
@SuppressWarnings("unchecked")
public class CacheUtils {
	
	private static final Map<String,CacheObject> cache =Maps.newConcurrentMap();
	
	/**
	 * set key value
	 * @param key
	 * @param data
	 * @param expireTime
	 */
	public static void set(String key,Object data, long expireTime){
		CacheObject ca = new CacheObject(key, data, expireTime);
		cache.put(key,ca);
	}
	
	/**
	 * 设置秒级缓存
	 * @param key
	 * @param data
	 * @param seconds
	 */
	public static void setWithSeconds(String key,Object data, int seconds){
		long expireTime = System.currentTimeMillis() + seconds * 1000;
		set(key, data, expireTime);
	}
	
	/**
	 * 设置分钟级别缓存
	 * @param key
	 * @param data
	 * @param minutes
	 */
	public static void setWithMinutes(String key,Object data, int minutes){
		long expireTime = System.currentTimeMillis() + minutes * 60 * 1000;
		set(key, data, expireTime);
	}
	
	/**
	 * 设置小时级别缓存
	 * @param key
	 * @param data
	 * @param hours
	 */
	public static void setWithHours(String key,Object data, int hours){
		long expireTime = System.currentTimeMillis() + hours * 60 * 60 * 1000;
		set(key, data, expireTime);
	}
	
	/**
	 * 设置天数级别缓存
	 * @param key
	 * @param data
	 * @param days
	 */
	public static void setwithDays(String key,Object data, int days){
		long expireTime = System.currentTimeMillis() + days * 24 * 60 * 60 * 1000;
		set(key, data, expireTime);
	}
	
	
	/**
	 * 设置不过时缓存
	 * @param key
	 * @param data
	 */
	public static void set(String key,Object data){
		set(key, data, -1);
	}
	
	/**
	 * get key value
	 * @param key
	 * @return
	 */
	public static <T> T get(String key){
		CacheObject thisCa = cache.get(key);
		if(thisCa.getExpireTime()<=0 || thisCa.getExpireTime()>System.currentTimeMillis()){
			return (T) thisCa.getValue();
		}
		//过时级别的缓存不在此处做销毁处理  以免销毁动作影响缓存带来的性能提升
		return null;
	}
	
	
	/**
	 * hset set value
	 * @param key<commit_msg>Update CacheObject.java<commit_after>package com.linuxea;

import java.util.Map;

import com.google.common.collect.Maps;

/**
 *@date      2017年8月7日 上午9:09:06
 *@desc      map实现缓存
 *@version   1.0
 *@author    Linuxea.lin
*/
@SuppressWarnings("unchecked")
public class CacheUtils {
	
	private static final Map<String,CacheObject> cache =Maps.newConcurrentMap();
	
	/**
	 * set key value
	 * @param key
	 * @param data
	 * @param expireTime
	 */
	public static void set(String key,Object data, long expireTime){
		CacheObject ca = new CacheObject(key, data, expireTime);
		cache.put(key,ca);
	}
	
	/**
	 * 设置秒级缓存
	 * @param key
	 * @param data
	 * @param seconds
	 */
	public static void setWithSeconds(String key,Object data, int seconds){
		long expireTime = System.currentTimeMillis() + seconds * 1000;
		set(key, data, expireTime);
	}
	
	/**
	 * 设置分钟级别缓存
	 * @param key
	 * @param data
	 * @param minutes
	 */
	public static void setWithMinutes(String key,Object data, int minutes){
		long expireTime = System.currentTimeMillis() + minutes * 60 * 1000;
		set(key, data, expireTime);
	}
	
	/**
	 * 设置小时级别缓存
	 * @param key
	 * @param data
	 * @param hours
	 */
	public static void setWithHours(String key,Object data, int hours){
		long expireTime = System.currentTimeMillis() + hours * 60 * 60 * 1000;
		set(key, data, expireTime);
	}
	
	/**
	 * 设置天数级别缓存
	 * @param key
	 * @param data
	 * @param days
	 */
	public static void setwithDays(String key,Object data, int days){
		long expireTime = System.currentTimeMillis() + days * 24 * 60 * 60 * 1000;
		set(key, data, expireTime);
	}
	
	
	/**
	 * 设置不过时缓存
	 * @param key
	 * @param data
	 */
	public static void set(String key,Object data){
		set(key, data, -1);
	}
	
	/**
	 * get key value
	 * @param key
	 * @return
	 */
	public static <T> T get(String key){
		CacheObject thisCa = cache.get(key);
		if(null!=thisCa && thisCa.getExpireTime()<=0 || thisCa.getExpireTime()>System.currentTimeMillis()){
			return (T) thisCa.getValue();
		}
		//过时级别的缓存不在此处做销毁处理  以免销毁动作影响缓存带来的性能提升
		return null;
	}
	
	
	/**
	 * hset set value
	 * @param key
{'fraction_non_alphanumeric': 0.13312469908521907, 'fraction_numerical': 0.020702936928261917, 'mean_word_length': 3.332638164754953, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6978136', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1553, 'n_words': 381}