001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.functions.cache; 020 021import java.io.IOException; 022 023import lucee.commons.io.cache.Cache; 024import lucee.commons.io.cache.CacheEntry; 025import lucee.runtime.PageContext; 026import lucee.runtime.config.ConfigImpl; 027import lucee.runtime.exp.PageException; 028import lucee.runtime.ext.function.Function; 029import lucee.runtime.op.Caster; 030import lucee.runtime.type.Collection; 031import lucee.runtime.type.KeyImpl; 032import lucee.runtime.type.Struct; 033import lucee.runtime.type.StructImpl; 034import lucee.runtime.type.util.KeyConstants; 035 036/** 037 * 038 */ 039public final class CacheGetMetadata implements Function { 040 041 private static final long serialVersionUID = -470089623854482521L; 042 043 private static final Collection.Key CACHE_HITCOUNT = KeyImpl.intern("cache_hitcount"); 044 private static final Collection.Key CACHE_MISSCOUNT = KeyImpl.intern("cache_misscount"); 045 private static final Collection.Key CACHE_CUSTOM = KeyImpl.intern("cache_custom"); 046 private static final Collection.Key CREATED_TIME = KeyImpl.intern("createdtime"); 047 private static final Collection.Key IDLE_TIME = KeyImpl.intern("idletime"); 048 private static final Collection.Key LAST_HIT = KeyImpl.intern("lasthit"); 049 private static final Collection.Key LAST_UPDATED = KeyImpl.intern("lastupdated"); 050 051 public static Struct call(PageContext pc, String id) throws PageException { 052 return call(pc, id,null); 053 } 054 055 public static Struct call(PageContext pc, String id, String cacheName) throws PageException { 056 try { 057 Cache cache = Util.getCache(pc,cacheName,ConfigImpl.CACHE_DEFAULT_OBJECT); 058 CacheEntry entry = cache.getCacheEntry(Util.key(id)); 059 060 Struct info=new StructImpl(); 061 info.set(CACHE_HITCOUNT, new Double(cache.hitCount())); 062 info.set(CACHE_MISSCOUNT, new Double(cache.missCount())); 063 info.set(CACHE_CUSTOM, cache.getCustomInfo()); 064 info.set(KeyConstants._custom, entry.getCustomInfo()); 065 066 info.set(CREATED_TIME, entry.created()); 067 info.set(KeyConstants._hitcount, new Double(entry.hitCount())); 068 info.set(IDLE_TIME, new Double(entry.idleTimeSpan())); 069 info.set(LAST_HIT, entry.lastHit()); 070 info.set(LAST_UPDATED, entry.lastModified()); 071 info.set(KeyConstants._size, new Double(entry.size())); 072 info.set(KeyConstants._timespan, new Double(entry.liveTimeSpan())); 073 return info; 074 } catch (IOException e) { 075 throw Caster.toPageException(e); 076 } 077 } 078}