1   package com.atlassian.user.impl.cache;
2   
3   import com.atlassian.user.User;
4   import com.atlassian.user.util.GenericCacheWrapper;
5   import com.atlassian.cache.*;
6   
7   import java.util.List;
8   
9   /**
10   * The groups-for-user cache is a cache where the key is the username as a
11   * {@link String} and the value is a {@link java.util.List} of group names as
12   * {@link String}s.
13   *
14   * @see CachingGroupManager
15   */
16  public class GroupsForUserCache
17  {
18      private final CacheFactory cacheFactory;
19      private final String cacheName;
20  
21      public GroupsForUserCache(CacheFactory cacheFactory, String cacheName)
22      {
23          this.cacheFactory = cacheFactory;
24          this.cacheName = cacheName;
25      }
26  
27      private GenericCacheWrapper<String, List<String>> getCache()
28      {
29          return new GenericCacheWrapper<String, List<String>>( cacheFactory.getCache(cacheName));
30      }
31  
32      public void put(User user, List<String> groupNames)
33      {
34          getCache().put(user.getName(), groupNames);
35      }
36  
37      public List<String> get(User user)
38      {
39          return getCache().get(user.getName());
40      }
41  
42      /**
43       * Remove the cached list of groups for a particular user from
44       * the cache. Typically used when the membership of a user has
45       * been updated, so the cached list of groups is out of date.
46       * <p/>
47       * It's not very efficient to iterate over the list and
48       * remove the affected groups, so we just ditch the entire
49       * cached list.
50       */
51      public void remove(User user)
52      {
53          getCache().remove(user.getName());
54      }
55  
56      /**
57       * Remove a list of users from the cache by username. Typically
58       * used when a group has been removed, and all its members need
59       * to have their cached groups cleared.
60       */
61      public void remove(List<String> usernames)
62      {
63          for (String username : usernames)
64          {
65              getCache().remove(username);
66          }
67      }
68  }