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