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

com.alachisoft.integrations.spring.CacheWrapper Maven / Gradle / Ivy

There is a newer version: 5.2-alpha-23
Show newest version
/*
 * Alachisoft (R) NCache Integrations
 * NCache Provider for Spring
 * ===============================================================================
 * Copyright © Alachisoft.  All rights reserved.
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 * ===============================================================================
 */

package com.alachisoft.integrations.spring;

import com.alachisoft.integrations.spring.configuration.SpringCacheConfiguration;
import com.alachisoft.ncache.client.CacheItem;
import com.alachisoft.ncache.client.CacheManager;
import com.alachisoft.ncache.runtime.caching.expiration.Expiration;
import com.alachisoft.ncache.runtime.caching.expiration.ExpirationType;
import com.alachisoft.ncache.runtime.caching.Tag;
import com.alachisoft.ncache.runtime.exceptions.CacheException;
import com.alachisoft.ncache.runtime.exceptions.ConfigurationException;
import com.alachisoft.ncache.runtime.exceptions.GeneralFailureException;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import com.alachisoft.ncache.runtime.exceptions.runtime.CacheRuntimeException;
import com.alachisoft.ncache.runtime.util.TimeSpan;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.cache.Cache;

public class CacheWrapper implements Cache {

    private com.alachisoft.integrations.spring.configuration.SpringCacheConfiguration cacheConfig = null;
    private com.alachisoft.ncache.client.Cache cache = null;
    private String cacheName = null;

    public CacheWrapper(String cacheName, SpringCacheConfiguration cacheConfig) throws CacheException, GeneralFailureException, Exception {
        this.cacheName = cacheName;
        this.cacheConfig = cacheConfig;
        cache = CacheManager.getCache(cacheConfig.getCacheidInstanceName());
    }

    @Override
    public String getName() {
        return cacheName;
    }

    @Override
    public Object getNativeCache() {
        return cache;
    }

    @Override
    public ValueWrapper get(Object key)  {
        CacheItem item = null;
        try {
            item = cache.getCacheItem(getKey(key));
        } catch (Exception ex) {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
        if (item != null) {
            try{
                return new com.alachisoft.integrations.spring.CacheObject(item.getValue(Object.class));
            }
            catch (Exception excep){
                return null;
            }
        }
        return null;
    }

    @Override
    public  T get(Object key, Class type) {
        CacheItem item = null;
        try {
            item = cache.getCacheItem(getKey(key));
            return item.getValue(type);
        } catch (Exception ex){
            throw new CacheRuntimeException(ex.hashCode(),ex.getMessage(),ex.getStackTrace().toString());
        }
    }

    @Override
    public  T get(Object o, Callable callable) {
        try {
           T val = (T) get(o, callable.call().getClass());
           if (val == null)
               put(o, callable.call());
           return  (val == null) ? callable.call() : val;
        }
        catch (Exception ex){
            throw new CacheRuntimeException(ex.hashCode(),ex.getMessage(),ex.getStackTrace().toString());
        }
    }

    @Override
    public void put(Object key, Object value) {
        try {
            cache.insert(getKey(key), getCacheItem(value));
        } catch (Exception ex) {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
    }

    @Override
    public ValueWrapper putIfAbsent(Object key, Object value) {
        CacheItem item = null;
        try
        {
            item = cache.getCacheItem(getKey(key));
            if (item == null)
            {
                put(key,value);
            }
            return new CacheObject(value);
        }
        catch (Exception ex)
        {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
        return  null;
    }

    @Override
    public void evict(Object key) {
        try {
            cache.remove(getKey(key), Object.class);
        } catch (Exception ex) {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
    }

    @Override
    public boolean evictIfPresent(Object key) {
        boolean result = false;
        try
        {
            CacheItem item  = getCacheItem(key);
            if(item == null)
                return result;
            evict(key);
            result = true;
        }
        catch (Exception ex)
        {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
        return result;
    }

    @Override
    public void clear() {
        try
        {
            cache.clear();
        }
        catch (Exception ex) {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
    }

    @Override
    public boolean invalidate() {
        boolean result = false;
        try{
            if(cache.getCount()>0)
            {
                clear();
                result = true;
            }
        }
        catch (Exception ex) {
            Logger.getLogger(CacheWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
        }
        return result;
    }

    private String getKey(Object key) {
        return cacheName + ":" + key.toString();
    }

    private CacheItem getCacheItem(Object value) {
        CacheItem cItem = new CacheItem(value);
        List tagList = new ArrayList();
        tagList.add(new Tag(cacheName));
        cItem.setTags(tagList);
        if (cacheConfig != null) {
            if (!cacheConfig.getExpirationType().isEmpty() && !cacheConfig.getExpirationType().toLowerCase().equals("none")) {
                Expiration expiration = new Expiration((cacheConfig.getExpirationType().equalsIgnoreCase("sliding"))? ExpirationType.Sliding : ExpirationType.Absolute, new TimeSpan(0, 0, cacheConfig.getExpirationPeriod()));
                cItem.setExpiration(expiration);
            }
        }
        return cItem;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy