doortts doortts 2016-04-18
cache: Fix userMap cache bugs
- Remove userMap cache from CacheStore
- Change sessionMap cache to be timid

It fix user related cache bugs.
eg. https://github.com/yona-projects/yona/issues/39
@4a6b42fa8e69bd2404faaa09e30100ea3a6119a9
app/controllers/UserApp.java
--- app/controllers/UserApp.java
+++ app/controllers/UserApp.java
@@ -374,8 +374,8 @@
             return invalidSession();
         }
         User user = null;
-        if (userKey != null){
-            user = CacheStore.sessionMap.get(userKey);
+        if (userKey != null && CacheStore.sessionMap.get(userKey) != null){
+            user = User.find.byId(CacheStore.sessionMap.get(userKey));
         }
         if (user == null) {
             return invalidSession();
@@ -878,7 +878,7 @@
     public static void addUserInfoToSession(User user) {
         String key = new Sha256Hash(new Date().toString(), ByteSource.Util.bytes(user.passwordSalt), 1024)
                 .toBase64();
-        CacheStore.sessionMap.put(key, user);
+        CacheStore.sessionMap.put(key, user.id);
         session(SESSION_USERID, String.valueOf(user.id));
         session(SESSION_LOGINID, user.loginId);
         session(SESSION_USERNAME, user.name);
app/models/AbstractPosting.java
--- app/models/AbstractPosting.java
+++ app/models/AbstractPosting.java
@@ -248,7 +248,7 @@
 
         actualWatchers.add(getAuthor());
         for (Comment c : getComments()) {
-            User user = User.findByIdUsingCache(c.authorId);
+            User user = User.find.byId(c.id);
             if (user != null) {
                 actualWatchers.add(user);
             }
app/models/NotificationEvent.java
--- app/models/NotificationEvent.java
+++ app/models/NotificationEvent.java
@@ -689,7 +689,7 @@
         }
 
         if (issue.assignee != null) {
-            notiEvent.newValue = User.findByIdUsingCache(issue.assignee.user.id).loginId;
+            notiEvent.newValue = User.find.byId(issue.assignee.user.id).loginId;
         }
         notiEvent.title = formatReplyTitle(issue);
         notiEvent.receivers = receivers;
app/models/User.java
--- app/models/User.java
+++ app/models/User.java
@@ -255,28 +255,6 @@
         return user.id;
     }
 
-
-    /**
-     * find a user by id with cache
-     *
-     * If there is no user correspond to login id string,
-     * then null
-     *
-     * @param id
-     * @return User or null
-     */
-    public static User findByIdUsingCache(Long id) {
-        User cached = CacheStore.userMap.get(Long.toString(id)); // special char + added to loginId key
-        if(cached != null) {
-            return cached;
-        }
-        User user = find.byId(id);
-        if(user != null){
-            CacheStore.userMap.putIfAbsent(Long.toString(id), user);
-        }
-        return user;
-    }
-
     /**
      * find a user by login id string
      *
@@ -287,16 +265,11 @@
      * @return User or {@link #anonymous}
      */
     public static User findByLoginId(String loginId) {
-        User cached = CacheStore.userMap.get("+" + loginId); // special char + added to loginId key
-        if(cached != null) {
-            return cached;
-        }
         User user = find.where().eq("loginId", loginId).findUnique();
         if (user == null) {
             return anonymous;
         }
         else {
-            CacheStore.userMap.putIfAbsent("+" + loginId, user);
             return user;
         }
     }
app/models/resource/Resource.java
--- app/models/resource/Resource.java
+++ app/models/resource/Resource.java
@@ -133,7 +133,7 @@
                 resource = Posting.finder.byId(longId).asResource();
                 break;
             case USER:
-                resource = User.findByIdUsingCache(longId).asResource();
+                resource = User.find.byId(longId).asResource();
                 break;
             case PROJECT:
                 resource = Project.find.byId(longId).asResource();
@@ -150,7 +150,7 @@
             case PULL_REQUEST:
                 return PullRequest.finder.byId(longId).asResource();
             case USER_AVATAR:
-                return User.findByIdUsingCache(longId).avatarAsResource();
+                return User.find.byId(longId).avatarAsResource();
             case REVIEW_COMMENT:
                 return ReviewComment.find.byId(longId).asResource();
             case ORGANIZATION:
app/utils/CacheStore.java
--- app/utils/CacheStore.java
+++ app/utils/CacheStore.java
@@ -2,7 +2,6 @@
 
 import com.google.common.cache.Cache;
 import com.google.common.cache.CacheBuilder;
-import models.User;
 
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -11,8 +10,7 @@
  * CacheStore
  */
 public class CacheStore {
-    public static Map<String, User> sessionMap = new ConcurrentHashMap<>();
-    public static Map<String, User> userMap = new ConcurrentHashMap<>();
+    public static Map<String, Long> sessionMap = new ConcurrentHashMap<>();
     public static Map<String, Long> projectMap = new ConcurrentHashMap<>();
     public static final int MAXIMUM_CACHED_MARKDOWN_ENTRY = 10000;
 
Add a comment
List