View Javadoc

1   package com.atlassian.user.impl.cache;
2   
3   import com.atlassian.user.repository.RepositoryIdentifier;
4   import com.atlassian.user.Entity;
5   import com.atlassian.cache.CacheFactory;
6   
7   /**
8    * The repository cache is a cache where the key is the entity name as a
9    * {@link String} and the value is the {@link com.atlassian.user.repository.RepositoryIdentifier} which
10   * contains the entity.
11   * <p/>
12   * This shouldn't be necessary once {@link com.atlassian.user.Entity} objects are aware
13   * of the repository which manages them.
14   * <p/>
15   * A separate cache for {@link com.atlassian.user.User} and {@link com.atlassian.user.Group}
16   * objects should be maintained, since they have a separate namespace for names.
17   */
18  public class EntityRepositoryCache
19  {
20      private final CacheFactory cacheFactory;
21      private final String cacheName;
22  
23      public EntityRepositoryCache(CacheFactory cacheFactory, String cacheName)
24      {
25          this.cacheFactory = cacheFactory;
26          this.cacheName = cacheName;
27      }
28  
29      private com.atlassian.cache.Cache getCache()
30      {
31          return cacheFactory.getCache(cacheName);
32      }
33  
34      public void put(Entity entity, RepositoryIdentifier repository)
35      {
36          getCache().put(entity.getName(), repository);
37      }
38  
39      /**
40       * Returns the repository containing the entity if found in the case,
41       * or null if this entity is not in the cache.
42       */
43      public RepositoryIdentifier get(Entity entity)
44      {
45          return (RepositoryIdentifier) getCache().get(entity.getName());
46      }
47  
48      public void remove(Entity entity)
49      {
50          getCache().remove(entity.getName());
51      }
52  }