fixed issue 'returned null for key'

This commit is contained in:
Sola
2015-12-26 15:30:36 +08:00
parent 1ce14b7d1a
commit 1e3a0754a4

View File

@@ -43,7 +43,8 @@ public class TableUser extends SQLCore {
public static User getByWechat(String wechat) {
try {
return cache.get(wechat);
User u = cache.get(wechat);
return u == NULL_USER ? null : u;
} catch (ExecutionException e) {
e.printStackTrace();
return null;
@@ -55,6 +56,7 @@ public class TableUser extends SQLCore {
s.beginTransaction();
s.update(user);
s.getTransaction().commit();
cache.put(user.getWechatId(), user);
return 1;
}
}
@@ -76,10 +78,13 @@ public class TableUser extends SQLCore {
private static class ValueLoader extends CacheLoader<String, User> {
@Override
public User load(String key) throws Exception {
return TableUser.getByWechat0(key);
User u = TableUser.getByWechat0(key);
return u == null ? NULL_USER : u;
}
}
private static final User NULL_USER = new User();
private static User getByWechat0(String wechat) {
try (Session s = sf.openSession()) {
return (User) s.createCriteria(User.class).add(Restrictions.eq(User.PROPERTY_WECHAT, wechat)).uniqueResult();