Search in sources :

Example 1 with Cache

use of lucee.commons.io.cache.Cache in project Lucee by lucee.

the class ObjectCache method _doStartTag.

public void _doStartTag() throws PageException, IOException {
    CacheHandlerCollection factory = null;
    Cache cache = null;
    if (type == TYPE_FUNCTION)
        factory = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_FUNCTION, null);
    else if (type == TYPE_INCLUDE)
        factory = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_INCLUDE, null);
    else if (type == TYPE_QUERY)
        factory = pageContext.getConfig().getCacheHandlerCollection(Config.CACHE_TYPE_QUERY, null);
    else if (type == TYPE_RESOURCE) {
        cache = CacheUtil.getDefault(pageContext, Config.CACHE_TYPE_RESOURCE, null);
        // no specific cache is defined, get default default cache
        if (cache == null) {
            // get cache resource provider
            CacheResourceProvider crp = null;
            ResourceProvider[] providers = ResourcesImpl.getGlobal().getResourceProviders();
            for (int i = 0; i < providers.length; i++) {
                if (providers[i].getScheme().equals("ram") && providers[i] instanceof CacheResourceProvider) {
                    crp = (CacheResourceProvider) providers[i];
                }
            }
            if (crp == null)
                throw new ApplicationException(Constants.NAME + " was not able to load the Ram Resource Provider");
            // get cache from resource provider
            cache = crp.getCache();
        }
    } else if (type == TYPE_OBJECT) {
        // throws a exception if not explicitly defined
        cache = CacheUtil.getDefault(pageContext, Config.CACHE_TYPE_OBJECT);
    } else if (type == TYPE_TEMPLATE) {
        // throws a exception if not explicitly defined
        cache = CacheUtil.getDefault(pageContext, Config.CACHE_TYPE_TEMPLATE);
    }
    // Clear
    if (action.equalsIgnoreCase("clear")) {
        if (filter == null) {
            if (cache != null)
                cache.remove(CacheKeyFilterAll.getInstance());
            else
                factory.clear(pageContext);
        } else {
            if (cache != null)
                CacheHandlerCollectionImpl.clear(pageContext, cache, filter);
            else
                factory.clear(pageContext, filter);
        }
    } else // Size
    if (action.equalsIgnoreCase("size")) {
        int size = 0;
        if (cache != null)
            size = cache.keys().size();
        else
            size = factory.size(pageContext);
        pageContext.setVariable(result, Caster.toDouble(size));
    } else
        throw new ApplicationException("attribute action has an invalid value [" + action + "], valid is only [clear,size]");
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) CacheHandlerCollection(lucee.runtime.cache.tag.CacheHandlerCollection) CacheResourceProvider(lucee.commons.io.res.type.cache.CacheResourceProvider) Cache(lucee.commons.io.cache.Cache)

Example 2 with Cache

use of lucee.commons.io.cache.Cache in project Lucee by lucee.

the class CacheClear method _call.

private static double _call(PageContext pc, Object filterOrTags, String cacheName) throws PageException {
    try {
        Object filter = FILTER;
        // tags
        boolean isArray = false;
        String dsn = null;
        if ((isArray = Decision.isArray(filterOrTags)) || Decision.isStruct(filterOrTags)) {
            // read tags from collection and datasource (optional)
            String[] tags;
            if (!isArray) {
                Struct sct = Caster.toStruct(filterOrTags);
                Array arr = Caster.toArray(sct.get("tags", null), null);
                if (arr == null)
                    throw new FunctionException(pc, "CacheClear", 1, "tags", "if you pass the tags within a struct, that struct need to have a key [tags] containing the tags in an array.");
                tags = ListUtil.toStringArray(arr);
                dsn = Caster.toString(sct.get(KeyConstants._datasource, null), null);
            } else {
                tags = ListUtil.toStringArray(Caster.toArray(filterOrTags));
            }
            // get default datasource
            if (StringUtil.isEmpty(dsn)) {
                Object tmp = pc.getApplicationContext().getDefDataSource();
                dsn = tmp instanceof CharSequence ? Caster.toString(tmp, null) : null;
            }
            filter = new QueryTagFilter(tags, StringUtil.isEmpty(dsn) ? null : dsn);
        } else // filter
        {
            String strFilter = Caster.toString(filterOrTags);
            if (CacheGetAllIds.isFilter(strFilter))
                filter = new WildCardFilter(strFilter, true);
        }
        Cache cache = CacheUtil.getCache(pc, cacheName, Config.CACHE_TYPE_OBJECT);
        if (filter instanceof CacheKeyFilter)
            return cache.remove((CacheKeyFilter) filter);
        return cache.remove((CacheEntryFilter) filter);
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
}
Also used : QueryTagFilter(lucee.runtime.cache.util.QueryTagFilter) FunctionException(lucee.runtime.exp.FunctionException) FunctionException(lucee.runtime.exp.FunctionException) PageException(lucee.runtime.exp.PageException) Struct(lucee.runtime.type.Struct) Array(lucee.runtime.type.Array) WildCardFilter(lucee.runtime.cache.util.WildCardFilter) CacheKeyFilter(lucee.commons.io.cache.CacheKeyFilter) Cache(lucee.commons.io.cache.Cache)

Example 3 with Cache

use of lucee.commons.io.cache.Cache in project Lucee by lucee.

the class CacheGetAllIds method call.

public static Array call(PageContext pc, String filter, String cacheName) throws PageException {
    try {
        Cache cache = CacheUtil.getCache(pc, cacheName, Config.CACHE_TYPE_OBJECT);
        List<String> keys = isFilter(filter) ? cache.keys(new WildCardFilter(filter, true)) : cache.keys();
        Iterator<String> it = keys.iterator();
        Array arr = new ArrayImpl();
        while (it.hasNext()) {
            arr.append(it.next());
        }
        return arr;
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
}
Also used : Array(lucee.runtime.type.Array) ArrayImpl(lucee.runtime.type.ArrayImpl) WildCardFilter(lucee.runtime.cache.util.WildCardFilter) FunctionException(lucee.runtime.exp.FunctionException) PageException(lucee.runtime.exp.PageException) Cache(lucee.commons.io.cache.Cache)

Example 4 with Cache

use of lucee.commons.io.cache.Cache in project Lucee by lucee.

the class CacheGetMetadata method call.

public static Struct call(PageContext pc, String id, String cacheName) throws PageException {
    try {
        Cache cache = CacheUtil.getCache(pc, cacheName, Config.CACHE_TYPE_OBJECT);
        CacheEntry entry = cache.getCacheEntry(CacheUtil.key(id));
        Struct info = new StructImpl();
        info.set(CACHE_HITCOUNT, new Double(cache.hitCount()));
        info.set(CACHE_MISSCOUNT, new Double(cache.missCount()));
        info.set(CACHE_CUSTOM, cache.getCustomInfo());
        info.set(KeyConstants._custom, entry.getCustomInfo());
        info.set(CREATED_TIME, entry.created());
        info.set(KeyConstants._hitcount, new Double(entry.hitCount()));
        info.set(IDLE_TIME, new Double(entry.idleTimeSpan()));
        info.set(LAST_HIT, entry.lastHit());
        info.set(LAST_UPDATED, entry.lastModified());
        info.set(KeyConstants._size, new Double(entry.size()));
        info.set(KeyConstants._timespan, new Double(entry.liveTimeSpan()));
        return info;
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
}
Also used : StructImpl(lucee.runtime.type.StructImpl) IOException(java.io.IOException) CacheEntry(lucee.commons.io.cache.CacheEntry) Cache(lucee.commons.io.cache.Cache) Struct(lucee.runtime.type.Struct)

Example 5 with Cache

use of lucee.commons.io.cache.Cache in project Lucee by lucee.

the class CachePut method _call.

private static String _call(PageContext pc, String key, Object value, Long timeSpan, Long idleTime, String cacheName) throws PageException {
    try {
        Cache cache = CacheUtil.getCache(pc, cacheName, Config.CACHE_TYPE_OBJECT);
        cache.put(CacheUtil.key(key), value, idleTime, timeSpan);
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
    return "";
}
Also used : FunctionException(lucee.runtime.exp.FunctionException) PageException(lucee.runtime.exp.PageException) Cache(lucee.commons.io.cache.Cache)

Aggregations

Cache (lucee.commons.io.cache.Cache)24 IOException (java.io.IOException)10 PageException (lucee.runtime.exp.PageException)8 RamCache (lucee.runtime.cache.ram.RamCache)7 ApplicationException (lucee.runtime.exp.ApplicationException)6 CacheEntry (lucee.commons.io.cache.CacheEntry)5 FunctionException (lucee.runtime.exp.FunctionException)4 Struct (lucee.runtime.type.Struct)4 CacheException (lucee.commons.io.cache.exp.CacheException)3 WildCardFilter (lucee.runtime.cache.util.WildCardFilter)3 Array (lucee.runtime.type.Array)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Config (lucee.runtime.config.Config)2 ConfigImpl (lucee.runtime.config.ConfigImpl)2 StructImpl (lucee.runtime.type.StructImpl)2 Date (java.util.Date)1 Iterator (java.util.Iterator)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 CacheEntryFilter (lucee.commons.io.cache.CacheEntryFilter)1