View Javadoc

1   /*
2    * Created by IntelliJ IDEA.
3    * User: Administrator
4    * Date: 10/07/2002
5    * Time: 14:31:20
6    * To change template for new class use
7    * Code Style | Class Templates options (Tools | IDE Options).
8    */
9   package com.atlassian.core.util;
10  
11  import com.opensymphony.module.oscache.base.Cache;
12  import com.opensymphony.module.oscache.web.ServletCacheAdministrator;
13  import org.apache.log4j.Category;
14  
15  import java.util.Date;
16  import java.util.Iterator;
17  import java.util.Map;
18  
19  /**
20   * Utility class to handle cache flushing.
21   */
22  public class CacheUtils
23  {
24      private static final Category log = Category.getInstance(CacheUtils.class);
25  
26      /**
27       * Flush the web tier caches given a key.
28       * <p>
29       * The different keys used are:
30       * assignee, status, summary, type, version, priority
31       */
32      public static void flushCache(String key)
33      {
34          try
35          {
36              Map applicationCaches = ServletCacheAdministrator.getApplicationCaches();
37              for (Iterator iterator = applicationCaches.entrySet().iterator(); iterator.hasNext();)
38              {
39                  Map.Entry entry = (Map.Entry) iterator.next();
40                  Cache cache = (Cache) entry.getValue();
41                  cache.flushPattern(key);
42              }
43  
44              Map sessionCaches = ServletCacheAdministrator.getSessionCaches();
45              for (Iterator iterator = sessionCaches.entrySet().iterator(); iterator.hasNext();)
46              {
47                  Map.Entry entry = (Map.Entry) iterator.next();
48                  Cache cache = (Cache) entry.getValue();
49                  cache.flushPattern(key);
50              }
51          }
52          catch (Exception e)
53          {
54              log.error("Error flushing caches matching key : " + key + " : " + e, e);
55          }
56      }
57  
58      /**
59       * Flush all the web tier caches
60       */
61      public static void flushAll()
62      {
63  
64          try
65          {
66              Map applicationCaches = ServletCacheAdministrator.getApplicationCaches();
67              for (Iterator iterator = applicationCaches.entrySet().iterator(); iterator.hasNext();)
68              {
69                  Map.Entry entry = (Map.Entry) iterator.next();
70                  Cache cache = (Cache) entry.getValue();
71                  cache.flushAll(new Date());
72              }
73  
74              Map sessionCaches = ServletCacheAdministrator.getSessionCaches();
75              for (Iterator iterator = sessionCaches.entrySet().iterator(); iterator.hasNext();)
76              {
77                  Map.Entry entry = (Map.Entry) iterator.next();
78                  Cache cache = (Cache) entry.getValue();
79                  cache.flushAll(new Date());
80              }
81          }
82          catch (Exception e)
83          {
84              log.error("Error flushing all caches: " + e, e);
85          }
86      }
87  }