Package org.nemesis.forum.util.cache

Examples of org.nemesis.forum.util.cache.Cache


  public ForumPermissions getPermissions(Authorization authorization) {
    int userID = authorization.getUserID();

    //Get the user perm cache for this forum
    Cache userPermCache = (Cache) getCacheManager().get(DbCacheManager.USER_PERMS_CACHE, new Integer(-1));

    //Simple case: if cache is turned on and the user is already cached,
    //we can simply return the cached permissions.
    if (userPermCache != null) {
      ForumPermissions permissions = (ForumPermissions) userPermCache.get(new Integer(userID));
      if (permissions != null) {
        return permissions;
      }
    }

    //Not so simple case: cache is not turned on or the user permissions
    //have not been cached yet.
    boolean isAnonymous = (userID == -1);
    boolean isUser = !isAnonymous;

    ForumPermissions finalPermissions = ForumPermissions.none();
    // check each forum for a specific permission
    Iterator allForums = this.forums();
    Forum forum;
    ForumPermissions forumUserPermissions;
    while (allForums.hasNext()) {
      forum = (Forum) allForums.next();
      forumUserPermissions = getUserPermissions(userID, forum.getID());
      finalPermissions = new ForumPermissions(finalPermissions, forumUserPermissions);
    }

    //Step 1 - Get permissions for the User. This includes anonymous
    //perms, "special user" perms, and the specific perms for the user.
    if (isUser) {
      ForumPermissions userPermissions = getUserPermissions(userID, -1);
      //Combine permissions
      finalPermissions = new ForumPermissions(finalPermissions, userPermissions);
    }
    //Add in anonymous perms.
    ForumPermissions anonyPermissions = null;
    if (userPermCache != null) {
      anonyPermissions = (ForumPermissions) userPermCache.get(new Integer(-1));
    }
    //Otherwise, do our own lookup.
    if (anonyPermissions == null) {
      anonyPermissions = getUserPermissions(-1, -1);
      //Add to cache so it will be there next time.
      if (userPermCache != null) {
        userPermCache.add(new Integer(-1), anonyPermissions);
      }
    }
    //Combine permissions
    finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);

    //If they are a valid user, figure out "any user" permissions.
    if (isUser) {
      ForumPermissions specialUserPermissions = null;
      //Check for cache
      if (userPermCache != null) {
        specialUserPermissions = (ForumPermissions) userPermCache.get(new Integer(0));
      }
      //Otherwise, do our own lookup.
      if (specialUserPermissions == null) {
        specialUserPermissions = getUserPermissions(0, -1);
        //Add to cache so it will be there next time.
        if (userPermCache != null) {
          userPermCache.add(new Integer(0), specialUserPermissions);
        }
      }
      //Combine permissions
      finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
    }

    //Step 2 -- get Permissions for all groups the user is in.
    int[] groups = ((DbProfileManager) getProfileManager()).getUserGroups(userID);
    for (int i = 0; i < groups.length; i++) {
      ForumPermissions groupPermissions = getGroupPermissions(groups[i], -1);
      finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
    }

    //Finally, add user to cache so it will be there next time.
    if (isUser && userPermCache != null) {
      userPermCache.add(new Integer(userID), finalPermissions);
    }

    return finalPermissions;
  }
View Full Code Here


  public ForumPermissions getPermissions(Authorization authorization) {
    int userID = authorization.getUserID();

    //Get the user perm cache for this forum
    Cache userPermCache = (Cache) factory.getCacheManager().get(DbCacheManager.USER_PERMS_CACHE, new Integer(id));

    //Simple case: if cache is turned on and the user is already cached,
    //we can simply return the cached permissions.
    if (userPermCache != null) {
      ForumPermissions permissions = (ForumPermissions) userPermCache.get(new Integer(userID));
      if (permissions != null) {
        return permissions;
      }
    }

    //Not so simple case: cache is not turned on or the user permissions
    //have not been cached yet.
    boolean isAnonymous = (userID == -1);
    boolean isUser = !isAnonymous;

    ForumPermissions finalPermissions = ForumPermissions.none();

    //Step 1 - Get permissions for the User. This includes anonymous
    //perms, "special user" perms, and the specific perms for the user.
    if (isUser) {
      ForumPermissions userPermissions = factory.getUserPermissions(userID, id);
      //Combine permissions
      finalPermissions = new ForumPermissions(finalPermissions, userPermissions);
    }
    //Add in anonymous perms.
    ForumPermissions anonyPermissions = null;
    if (userPermCache != null) {
      anonyPermissions = (ForumPermissions) userPermCache.get(new Integer(-1));
    }
    //Otherwise, do our own lookup.
    if (anonyPermissions == null) {
      anonyPermissions = factory.getUserPermissions(-1, id);
      //Add to cache so it will be there next time.
      if (userPermCache != null) {
        userPermCache.add(new Integer(-1), anonyPermissions);
      }
    }
    //Combine permissions
    finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);

    //If they are a valid user, figure out "any user" permissions.
    if (isUser) {
      ForumPermissions specialUserPermissions = null;
      //Check for cache
      if (userPermCache != null) {
        specialUserPermissions = (ForumPermissions) userPermCache.get(new Integer(0));
      }
      //Otherwise, do our own lookup.
      if (specialUserPermissions == null) {
        specialUserPermissions = factory.getUserPermissions(0, id);
        //Add to cache so it will be there next time.
        if (userPermCache != null) {
          userPermCache.add(new Integer(0), specialUserPermissions);
        }
      }
      //Combine permissions
      finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
    }

    //Step 2 -- get Permissions for all groups the user is in.
    int[] groups = ((DbProfileManager) factory.getProfileManager()).getUserGroups(userID);
    for (int i = 0; i < groups.length; i++) {
      ForumPermissions groupPermissions = factory.getGroupPermissions(groups[i], id);
      finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
    }

    //Finally, add user to cache so it will be there next time.
    if (isUser && userPermCache != null) {
      userPermCache.add(new Integer(userID), finalPermissions);
    }

    return finalPermissions;
  }
View Full Code Here

          } catch (Exception e) {
          }
        }
        if(request.getParameter("newSize")!=null){
          try {
            Cache cache = cacheManager.getCache(Integer.parseInt(request.getParameter("oid")));
            cache.setMaxSize(Integer.parseInt(request.getParameter("newSize"))*1024);
          } catch (Exception e) {
          }
        }
       
        //init
View Full Code Here

    int HOUR = MINUTE * 60;

    caches = new Cache[9];

    //Initialize all cache structures
    caches[USER_CACHE] = new Cache(256 * 1024, 6 * HOUR);
    caches[USER_ID_CACHE] = new Cache(128 * 1024, 6 * HOUR);
    caches[GROUP_CACHE] = new Cache(128 * 1024, 6 * HOUR);
    caches[GROUP_ID_CACHE] = new Cache(128 * 1024, 6 * HOUR);
    caches[FORUM_CACHE] = new Cache(128 * 1024, 6 * HOUR);
    caches[FORUM_ID_CACHE] = new Cache(128 * 1024, 6 * HOUR);
    caches[THREAD_CACHE] = new Cache(128 * 1024, 6 * HOUR);
    caches[MESSAGE_CACHE] = new Cache(512 * 1024, 6 * HOUR);
    //The user permissions cache is a special one. It's actually a Cache
    //of Cache objects. Each of the cache objects in the main cache
    //corresponds to a particular forum, and is used to cache the
    //permissions that a user has for a forum. In order to handle this
    //requirement, we use a special subclass of Cache.
View Full Code Here

  }

  public void removeUserPerm(Object userID) {
    Object[] values = caches[USER_PERMS_CACHE].values().toArray();
    for (int i = 0; i < values.length; i++) {
      Cache cache = (Cache) ((CacheObject) values[i]).object;
      cache.remove(userID);
    }
    //when cache becomes distributed, we'd send out an expire message
    //here to all other yazd servers.
  }
View Full Code Here

    //when cache becomes distributed, we'd send out an expire message
    //here to all other yazd servers.
  }

  public void removeUserPerm(Object userID, Object forumID) {
    Cache cache = (Cache) caches[USER_PERMS_CACHE].get(forumID);
    if (cache != null) {
      cache.remove(userID);
    }
    //when cache becomes distributed, we'd send out an expire message
    //here to all other yazd servers.
  }
View Full Code Here

  public UserPermsCache(int size, long expireTime) {
    super(size, expireTime);
  }

  public synchronized Cacheable get(Object key) {
    Cache subCache = (Cache) super.get(key);
    if (subCache == null) {
      //cache has expired, or is not there, so put a new one in there.
      //Cache objects only need to last as long as a user's session
      //does. Half an hour is a reasonable amount of time for this.
      subCache = new Cache(2 * 1024, 30 * 1000 * 60);
      add(key, subCache);
    }
    return subCache;
  }
View Full Code Here

   */
  public int getSize() {
    int size = 0;
    Object[] values = values().toArray();
    for (int i = 0; i < values.length; i++) {
      Cache cache = (Cache) values[i];
      size += cache.getSize();
    }
    return size;
  }
View Full Code Here

TOP

Related Classes of org.nemesis.forum.util.cache.Cache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.