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

com.jeesuite.security.cache.LocalCache Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.jeesuite.security.cache;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.CacheBuilder;
import com.jeesuite.security.Cache;

public class LocalCache implements Cache{
	
	private com.google.common.cache.Cache cache;

	public LocalCache(int timeToLiveSeconds) {
		cache =  CacheBuilder
				.newBuilder()
				.maximumSize(10000)
				.expireAfterWrite(timeToLiveSeconds, TimeUnit.SECONDS)
				.build();
	}

	@Override
	public void setString(String key, String value) {
		cache.put(key, value);
	}

	@Override
	public String getString(String key) {
		return Objects.toString(getObject(key), null);
	}

	@Override
	public void setObject(String key, Object value) {
		cache.put(key, value);
	}

	@Override
	public  T getObject(String key) {
		return (T) cache.getIfPresent(key);
	}

	@Override
	public void remove(String key) {
		cache.invalidate(key);
	}

	@Override
	public boolean exists(String key) {
		return cache.getIfPresent(key) != null;
	}

	@Override
	public void removeAll() {
		cache.invalidateAll();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy