1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.lesscss.internal.cache

File AbstractCache.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
31
9
1
178
96
13
0.42
3.44
9
1.44

Classes

Class Line # Actions
AbstractCache 43 31 0% 13 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.lesscss.internal.cache;
21   
22    import java.util.ArrayList;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.inject.Inject;
28   
29    import org.xwiki.cache.Cache;
30    import org.xwiki.cache.CacheManager;
31    import org.xwiki.lesscss.internal.colortheme.ColorThemeReference;
32    import org.xwiki.lesscss.resources.LESSResourceReference;
33    import org.xwiki.lesscss.internal.skin.SkinReference;
34   
35    /**
36    * Default and abstract implementation of {@link org.xwiki.lesscss.internal.cache.LESSCache}.
37    *
38    * @param <T> class of the object to cache
39    *
40    * @since 6.4M2
41    * @version $Id: 6a135f296445ecc83b6cd1304c4c3d1bffeef81c $
42    */
 
43    public abstract class AbstractCache<T> implements LESSCache<T>
44    {
45    @Inject
46    protected CacheManager cacheManager;
47   
48    /**
49    * Whether or not the cache should handle the current XWikiContext object (true by default).
50    */
51    protected boolean isContextHandled = true;
52   
53    /**
54    * The cache that will store the content.
55    */
56    protected Cache<T> cache;
57   
58    /**
59    * This map stores the list of the cached files keys corresponding to a skin, in order to clear the corresponding
60    * cache when a skin is saved.
61    */
62    private Map<Object, List<String>> cachedFilesKeysMapPerSkin = new HashMap<>();
63   
64    /**
65    * This map stores the list of the cached files keys corresponding to a color theme, in order to clear the
66    * corresponding cache when a color theme is saved.
67    */
68    private Map<Object, List<String>> cachedFilesKeysMapPerColorTheme = new HashMap<>();
69   
70    /**
71    * This map stores the list of the cached files keys corresponding to a LESS resource, in order to clear the
72    * corresponding cache when a LESS resource is saved.
73    */
74    private Map<Object, List<String>> cachedFilesKeysMapPerLESSResource = new HashMap<>();
75   
76    @Inject
77    private CacheKeyFactory cacheKeyFactory;
78   
79    private Map<String, String> mutexList = new HashMap<>();
80   
 
81  2164 toggle @Override
82    public T get(LESSResourceReference lessResourceReference, SkinReference skin, ColorThemeReference colorTheme)
83    {
84  2164 return cache.get(cacheKeyFactory.getCacheKey(lessResourceReference, skin, colorTheme, isContextHandled));
85    }
86   
 
87  82 toggle @Override
88    public void set(LESSResourceReference lessResourceReference, SkinReference skin,
89    ColorThemeReference colorTheme, T content)
90    {
91    // Store the content in the cache
92  82 String cacheKey = cacheKeyFactory.getCacheKey(lessResourceReference, skin, colorTheme, isContextHandled);
93  82 cache.set(cacheKey, content);
94   
95    // Add the new key to maps
96  82 registerCacheKey(cachedFilesKeysMapPerSkin, cacheKey, skin);
97  82 registerCacheKey(cachedFilesKeysMapPerColorTheme, cacheKey, colorTheme);
98  82 registerCacheKey(cachedFilesKeysMapPerLESSResource, cacheKey, lessResourceReference);
99    }
100   
101    /**
102    * Add the cache key in the specified map (cachedFilesKeysMapPerSkin or cachedFilesKeysMapPerColorTheme), to be
103    * able to clear the cache when one skin or one color theme is modified.
104    *
105    * @param cachedFilesKeysMap could be cachedFilesKeysMapPerSkin or cachedFilesKeysMapPerColorTheme
106    * @param cacheKey the cache key to register
107    * @param reference name of the skin or of the color theme
108    */
 
109  246 toggle private void registerCacheKey(Map<Object, List<String>> cachedFilesKeysMap, String cacheKey, Object reference)
110    {
111  246 List<String> cachedFilesKeys = cachedFilesKeysMap.get(reference);
112  246 if (cachedFilesKeys == null) {
113    // if the list of cached files keys corresponding to the skin/colortheme name does not exist, we create it
114  204 cachedFilesKeys = new ArrayList<>();
115  204 cachedFilesKeysMap.put(reference, cachedFilesKeys);
116    }
117  246 if (!cachedFilesKeys.contains(cacheKey)) {
118  237 cachedFilesKeys.add(cacheKey);
119    }
120    }
121   
 
122  1 toggle @Override
123    public void clear()
124    {
125  1 cache.removeAll();
126  1 cachedFilesKeysMapPerSkin.clear();
127  1 cachedFilesKeysMapPerColorTheme.clear();
128  1 cachedFilesKeysMapPerLESSResource.clear();
129    }
130   
 
131  17 toggle private void clearFromCriteria(Map<Object, List<String>> cachedFilesKeysMap, Object criteria)
132    {
133    // Get the list of cached files keys corresponding to the criteria
134  17 List<String> cachedFilesKeys = cachedFilesKeysMap.get(criteria);
135  17 if (cachedFilesKeys == null) {
136  14 return;
137    }
138    // Remove all the cached files corresponding to the cached keys
139  3 for (String cachedFileKey : cachedFilesKeys) {
140  6 cache.remove(cachedFileKey);
141    }
142    // Remove the list of cached keys corresponding to the criteria
143  3 cachedFilesKeysMap.remove(criteria);
144    }
145   
 
146  1 toggle @Override
147    public void clearFromSkin(SkinReference skin)
148    {
149  1 clearFromCriteria(cachedFilesKeysMapPerSkin, skin);
150    }
151   
 
152  13 toggle @Override
153    public void clearFromColorTheme(ColorThemeReference colorTheme)
154    {
155  13 clearFromCriteria(cachedFilesKeysMapPerColorTheme, colorTheme);
156    }
157   
 
158  3 toggle @Override
159    public void clearFromLESSResource(LESSResourceReference lessResourceReference)
160    {
161  3 clearFromCriteria(cachedFilesKeysMapPerLESSResource, lessResourceReference);
162    }
163   
 
164  2163 toggle @Override
165    public synchronized Object getMutex(LESSResourceReference lessResourceReference, SkinReference skin,
166    ColorThemeReference colorTheme)
167    {
168    // The mutex is a string (actually the cache key) to help debugging.
169  2163 String cacheKey = cacheKeyFactory.getCacheKey(lessResourceReference, skin, colorTheme, isContextHandled);
170  2163 String mutex = mutexList.get(cacheKey);
171  2163 if (mutex == null) {
172    // the mutex is the key, so no extra memory is needed
173  69 mutex = cacheKey;
174  69 mutexList.put(cacheKey, mutex);
175    }
176  2163 return mutex;
177    }
178    }