com.deviceatlas.cloud.deviceidentification.service.CacheService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deviceatlas-cloud-java-client Show documentation
Show all versions of deviceatlas-cloud-java-client Show documentation
DeviceAtlas is the world's fastest, most accurate device detection solution
providing real-time information on all mobile and other devices accessing the web.
The newest version!
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Afilias Technologies Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.deviceatlas.cloud.deviceidentification.service;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheException;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheProvider;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.FileCacheProvider;
import com.deviceatlas.cloud.deviceidentification.endpoint.EndPoint;
import com.deviceatlas.cloud.deviceidentification.client.ClientConstants;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class CacheService {
/**
* Please see ClientConstants.CACHE_NAME.toString()
* @deprecated
*/
@Deprecated public static final String CACHE_NAME = ClientConstants.CACHE_NAME.toString();
/**
* Please see ClientConstants.CACHE_NAME_SERVERS_AUTO.toString()
* @deprecated
*/
@Deprecated public static final String CACHE_NAME_SERVERS_AUTO = ClientConstants.CACHE_NAME_SERVERS_AUTO.toString();
/**
* Please see ClientConstants.CACHE_NAME_SERVERS_MANUAL.toString()
* @deprecated
*/
@Deprecated public static final String CACHE_NAME_SERVERS_MANUAL = ClientConstants.CACHE_NAME_SERVERS_MANUAL.toString();
private int serverRankListLifetime = 1440;
private boolean useCache = true;
private CacheProvider cachePropsProvider;
private CacheProvider cacheServersRankingProvider;
public CacheService(CacheProvider cachePropsProvider) throws CacheException {
this.cachePropsProvider = cachePropsProvider;
this.cacheServersRankingProvider = new FileCacheProvider();
cacheServersRankingProvider.setExpiry(serverRankListLifetime);
}
/**
* Returns the server ranking cache lifetime
*
* @return serverRankingLifetime
*/
public int getServerRankingLifetime() {
return serverRankListLifetime;
}
/**
* Sets the server ranking cache lifetime
*
* @param serverRankListLifetime
*/
public void setServerRankingLifetime(int serverRankListLifetime) {
this.serverRankListLifetime = serverRankListLifetime;
}
/**
* Gets the cache Provider
*
* @return cachePropsProvider
*/
public CacheProvider getCacheProvider() {
return cachePropsProvider;
}
/**
* Is device data being cached by the API or not.
*
* @return true(default) = data is being cached.
*/
public boolean getUseCache() {
return useCache;
}
/**
* Setter to cache or not to cache device data.
*
* @param useCache true = cache data after getting device data from DeviceAtlas cloud
*/
public void setUseCache(boolean useCache) {
this.useCache = useCache;
}
/**
* Clear all cached data.
*/
public void clearCache() {
cachePropsProvider.clear();
cacheServersRankingProvider.clear();
}
/**
* This should be called when shutting down your application. It asks
* the cache manager to shutdown the cache and write the cache to disk.
*
* See http://ehcache.org/documentation/code-samples#shutdown-the-cachemanager
*/
public void shutdown() {
this.cachePropsProvider.shutdown();
this.cacheServersRankingProvider.shutdown();
}
/**
* Get cached server list. For debugging cache contents.
*
* @param cacheKey cache key Client.CACHE_NAME_SERVERS_AUTO or Client.CACHE_NAME_SERVERS_MANUAL
* @return array of EndPoint objects or null if cache is empty
*/
public EndPoint[] getCachedServerList(String cacheKey) throws CacheException {
EndPoint[] cachedEndPoints = null;
if (cacheKey.equals(ClientConstants.CACHE_NAME_SERVERS_AUTO.toString())) {
List