001/* 002 * Copyright 2010 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.plugins.tagcloud.cache; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.component.Component; 022 023import org.ametys.runtime.plugin.component.AbstractLogEnabled; 024 025/** 026 * This class handles the cache used by the tag cloud plugin 027 */ 028public class TagCloudCacheManager extends AbstractLogEnabled implements Component 029{ 030 /** Avalon role */ 031 public static final String ROLE = TagCloudCacheManager.class.getName(); 032 033 // Cache by workspace and zone item 034 private Map<String, Map<String, Object>> _objects = new HashMap<>(); 035 036 /** 037 * Add tag cloud in cache 038 * @param workspaceName The workspace name 039 * @param zoneItemId The zone item id 040 * @param object The tag cloud object itself 041 */ 042 public void addTagCloud (String workspaceName, String zoneItemId, Object object) 043 { 044 if (!_objects.containsKey(workspaceName)) 045 { 046 _objects.put(workspaceName, new HashMap<>()); 047 } 048 049 _objects.get(workspaceName).put(zoneItemId, object); 050 } 051 052 /** 053 * Get the tag cloud in cache or null if it is not in cache. 054 * @param workspaceName The workspace name 055 * @param zoneItemId The zone item id 056 * @return The tag cloud 057 */ 058 public Object getTagCloud (String workspaceName, String zoneItemId) 059 { 060 if (_objects.containsKey(workspaceName)) 061 { 062 return _objects.get(workspaceName).get(zoneItemId); 063 } 064 return null; 065 } 066 067 /** 068 * Determines if the tag cloud is in cache 069 * @param workspaceName The workspace name 070 * @param zoneItemId The zone item id 071 * @return true if the tag cloud is in cache 072 */ 073 public boolean hasTagCloud (String workspaceName, String zoneItemId) 074 { 075 return _objects.containsKey(workspaceName) && _objects.get(workspaceName).containsKey(zoneItemId); 076 } 077 078 /** 079 * Invalid tag cloud for all workspaces 080 * @param zoneItemId the zone item id 081 */ 082 public void invalidateTagCloud (String zoneItemId) 083 { 084 for (String workspaceName : _objects.keySet()) 085 { 086 _objects.get(workspaceName).remove(zoneItemId); 087 } 088 } 089 090 /** 091 * Invalid cache for all workspaces 092 */ 093 public void invalidateCache () 094 { 095 _objects = new HashMap<>(); 096 } 097 098}