Skip to content

Commit cd2b179

Browse files
committed
b3log#12319 缓存管理员
1 parent 69947b6 commit cd2b179

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/main/java/org/b3log/solo/cache/UserCache.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.b3log.latke.cache.CacheFactory;
2121
import org.b3log.latke.ioc.inject.Named;
2222
import org.b3log.latke.ioc.inject.Singleton;
23+
import org.b3log.latke.model.Role;
2324
import org.b3log.latke.model.User;
2425
import org.b3log.solo.util.JSONs;
2526
import org.json.JSONObject;
@@ -28,7 +29,7 @@
2829
* User cache.
2930
*
3031
* @author <a href="http://88250.b3log.org">Liang Ding</a>
31-
* @version 1.0.0.0, Jul 22, 2017
32+
* @version 1.1.0.0, Aug 27, 2017
3233
* @since 2.3.0
3334
*/
3435
@Named
@@ -45,6 +46,29 @@ public class UserCache {
4546
*/
4647
private static final Cache EMAIL_CACHE = CacheFactory.getCache(User.USERS + "Email");
4748

49+
/**
50+
* Admin user.
51+
*/
52+
private static final Cache ADMIN_CACHE = CacheFactory.getCache("adminUser");
53+
54+
/**
55+
* Gets the admin user.
56+
*
57+
* @return admin user
58+
*/
59+
public JSONObject getAdmin() {
60+
return ADMIN_CACHE.get(Role.ADMIN_ROLE);
61+
}
62+
63+
/**
64+
* Adds or updates the admin user.
65+
*
66+
* @param admin the specified admin user
67+
*/
68+
public void putAdmin(final JSONObject admin) {
69+
ADMIN_CACHE.put(Role.ADMIN_ROLE, admin);
70+
}
71+
4872
/**
4973
* Gets a user by the specified user id.
5074
*

src/main/java/org/b3log/solo/repository/impl/UserRepositoryImpl.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* User repository.
3131
*
3232
* @author <a href="http://88250.b3log.org">Liang Ding</a>
33-
* @version 1.1.0.8, Jul 22, 2017
33+
* @version 1.1.0.9, Aug 27, 2017
3434
* @since 0.3.1
3535
*/
3636
@Repository
@@ -79,6 +79,10 @@ public void update(final String id, final JSONObject user) throws RepositoryExce
7979

8080
user.put(Keys.OBJECT_ID, id);
8181
userCache.putUser(user);
82+
83+
if (Role.ADMIN_ROLE.equals(user.optString(User.USER_ROLE))) {
84+
userCache.putAdmin(user);
85+
}
8286
}
8387

8488
@Override
@@ -105,14 +109,22 @@ public JSONObject getByEmail(final String email) throws RepositoryException {
105109

106110
@Override
107111
public JSONObject getAdmin() throws RepositoryException {
112+
JSONObject ret = userCache.getAdmin();
113+
if (null != ret) {
114+
return ret;
115+
}
116+
108117
final Query query = new Query().setFilter(new PropertyFilter(User.USER_ROLE, FilterOperator.EQUAL, Role.ADMIN_ROLE)).setPageCount(1);
109118
final JSONObject result = get(query);
110119
final JSONArray array = result.optJSONArray(Keys.RESULTS);
111120
if (0 == array.length()) {
112121
return null;
113122
}
114123

115-
return array.optJSONObject(0);
124+
ret = array.optJSONObject(0);
125+
userCache.putAdmin(ret);
126+
127+
return ret;
116128
}
117129

118130
@Override

0 commit comments

Comments
 (0)