001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.util; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Timer; 021import java.util.TimerTask; 022 023import org.apache.avalon.framework.activity.Disposable; 024import org.apache.avalon.framework.activity.Initializable; 025 026import org.ametys.runtime.plugin.component.AbstractLogEnabled; 027 028/** 029 * Simple memory cache with a thread clearing the cache every day. 030 * @param <T> the type of objects cached by this component. 031 */ 032public class CachingComponent<T> extends AbstractLogEnabled implements Initializable, Disposable 033{ 034 private Map<String, T> _objects = new HashMap<>(); 035 private Timer _timer; 036 037 public void initialize() throws Exception 038 { 039 if (isCacheEnabled()) 040 { 041 _timer = new Timer("CachingComponent", true); 042 043 long period = 1000 * 60 * 60 * 24; // one day 044 _timer.scheduleAtFixedRate(new TimerTask() 045 { 046 @Override 047 public void run() 048 { 049 clearCache(); 050 } 051 }, period, period); 052 } 053 } 054 055 public void dispose() 056 { 057 if (_timer != null) 058 { 059 _timer.cancel(); 060 } 061 } 062 063 /** 064 * Returns an object from the cache, correspondong to the specified key, or null if none. 065 * @param key the object's key. 066 * @return the object from cache. 067 */ 068 protected T getObjectFromCache(String key) 069 { 070 T object = _objects.get(key); 071 072 if (getLogger().isDebugEnabled()) 073 { 074 getLogger().debug("Getting object" + object + "from cache for key " + key); 075 } 076 077 return object; 078 } 079 080 /** 081 * Adds a key/object pair in the cache. 082 * @param key the object's key. 083 * @param object the object to be cached. 084 */ 085 protected void addObjectInCache(String key, T object) 086 { 087 if (getLogger().isDebugEnabled()) 088 { 089 getLogger().debug("Adding object " + object + "in cache for key " + key); 090 } 091 092 _objects.put(key, object); 093 } 094 095 /** 096 * Removes a key/object pair in the cache. 097 * @param key the object's key. 098 */ 099 protected void removeObjectFromCache(String key) 100 { 101 if (getLogger().isDebugEnabled()) 102 { 103 getLogger().debug("Removing object in cache for key " + key); 104 } 105 106 _objects.remove(key); 107 } 108 109 /** 110 * Removes all entries from the cache. 111 */ 112 protected void clearCache() 113 { 114 if (getLogger().isDebugEnabled()) 115 { 116 getLogger().debug("Clearing cache"); 117 } 118 119 _objects.clear(); 120 } 121 122 /** 123 * Returns true if the cache is enabled. 124 * @return true if the cache is enabled. 125 */ 126 protected boolean isCacheEnabled() 127 { 128 return true; 129 } 130}