From fc2aafbe481754420fc49cf244fdb10d9ab20786 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 14 Jul 2015 17:10:51 +0200 Subject: [client] Add user cache --- .../java/org/openslx/dozmod/thrift/UserCache.java | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java new file mode 100644 index 00000000..50c02f08 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/UserCache.java @@ -0,0 +1,71 @@ +package org.openslx.dozmod.thrift; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.openslx.bwlp.thrift.iface.UserInfo; +import org.openslx.thrifthelper.ThriftManager; +import org.openslx.util.GenericDataCache; + +public class UserCache { + + private static final Logger LOGGER = Logger.getLogger(UserCache.class); + + /** + * How long should the list be cached? + */ + private static final int CACHE_TIME_MS = 5 * 60 * 1000; + + private static final GenericDataCache> cache = new GenericDataCache>( + CACHE_TIME_MS) { + + @Override + protected List update() throws Exception { + List result = null; + int pageSize = ThriftManager.getSatClient().getPageSize(); + for (int i = 0;; ++i) { + List page = ThriftManager.getSatClient() + .getUserList(Session.getSatelliteToken(), i); + if (result == null) { + result = page; + } else { + result.addAll(page); + } + if (page.size() < pageSize) + break; + } + return result; + } + }; + + private UserCache() { + // No instancing + } + + /** + * Get all known users + * + * @return list of users + */ + public static List getAll() { + return cache.get(); + } + + /** + * Find the user with the given id + * + * @param user id + * @return matching user, or null if not found + */ + public static UserInfo find(String userId) { + List list = cache.get(); + if (list == null) + return null; + for (UserInfo user : list) { + if (user.userId.equals(userId)) + return user; + } + return null; + } + +} -- cgit v1.2.3-55-g7522