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]");
}
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);
}
}
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);
}
}
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);
}
}
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 "";
}
Aggregations