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.cache.tag.request; 020 021import java.util.HashMap; 022import java.util.Iterator; 023import java.util.Map; 024import java.util.Map.Entry; 025 026import lucee.runtime.PageContext; 027import lucee.runtime.cache.tag.CacheHandler; 028import lucee.runtime.cache.tag.CacheHandlerFilter; 029import lucee.runtime.cache.tag.CacheItem; 030import lucee.runtime.exp.PageException; 031 032public class RequestCacheHandler implements CacheHandler { 033 034 private static ThreadLocal<Map<String,CacheItem>> data=new ThreadLocal<Map<String,CacheItem>>() { 035 @Override 036 protected Map<String,CacheItem> initialValue() { 037 return new HashMap<String, CacheItem>(); 038 } 039 }; 040 private final int cacheType; 041 042 public RequestCacheHandler(int cacheType) { 043 this.cacheType=cacheType; 044 } 045 046 @Override 047 public CacheItem get(PageContext pc, String id) { 048 return data.get().get(id); 049 } 050 051 @Override 052 public boolean remove(PageContext pc, String id) { 053 return data.get().remove(id)!=null; 054 } 055 056 @Override 057 public void set(PageContext pc, String id,Object cachedwithin, CacheItem value) { 058 // cachedwithin is ignored in this cache, it should be "request" 059 data.get().put(id,value); 060 } 061 062 @Override 063 public void clear(PageContext pc) { 064 data.get().clear(); 065 } 066 067 @Override 068 public void clear(PageContext pc, CacheHandlerFilter filter) { 069 Iterator<Entry<String, CacheItem>> it = data.get().entrySet().iterator(); 070 Entry<String, CacheItem> e; 071 while(it.hasNext()){ 072 e = it.next(); 073 if(filter==null || filter.accept(e.getValue())) 074 it.remove(); 075 } 076 } 077 078 @Override 079 public int size(PageContext pc) { 080 return data.get().size(); 081 } 082 083 @Override 084 public void clean(PageContext pc) { 085 // not necessary 086 } 087 088 @Override 089 public String label() throws PageException { 090 return "request"; 091 } 092 093}