diff options
author | Abyss777 <abyss@fox5.ru> | 2016-08-23 14:48:01 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-08-23 14:48:01 +0500 |
commit | 21bf1014e55ab8049e18ee991c6ee80b23d1789c (patch) | |
tree | 1b69c9e0e303773fc5d85958ac6bd369471b9637 /src/org/traccar/database | |
parent | 5536fbe00c022c151007ad077d911b5162201cd4 (diff) | |
download | trackermap-server-21bf1014e55ab8049e18ee991c6ee80b23d1789c.tar.gz trackermap-server-21bf1014e55ab8049e18ee991c6ee80b23d1789c.tar.bz2 trackermap-server-21bf1014e55ab8049e18ee991c6ee80b23d1789c.zip |
Implement cache for users objects
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/PermissionsManager.java | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 3c2cc252b..f5fed978a 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -25,10 +25,12 @@ import org.traccar.model.Server; import org.traccar.model.User; import java.sql.SQLException; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; public class PermissionsManager { @@ -36,7 +38,7 @@ public class PermissionsManager { private volatile Server server; - private final Map<Long, User> users = new HashMap<>(); + private final Map<Long, User> users = new ConcurrentHashMap<>(); private final Map<Long, Set<Long>> groupPermissions = new HashMap<>(); private final Map<Long, Set<Long>> devicePermissions = new HashMap<>(); @@ -73,19 +75,26 @@ public class PermissionsManager { public PermissionsManager(DataManager dataManager) { this.dataManager = dataManager; - refresh(); + refreshUsers(); + refreshPermissions(); } - public final void refresh() { + public final void refreshUsers() { users.clear(); - groupPermissions.clear(); - devicePermissions.clear(); try { server = dataManager.getServer(); for (User user : dataManager.getUsers()) { users.put(user.getId(), user); } + } catch (SQLException error) { + Log.warning(error); + } + } + public final void refreshPermissions() { + groupPermissions.clear(); + devicePermissions.clear(); + try { GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), Context.getDeviceManager().getAllDevices()); for (GroupPermission permission : dataManager.getGroupPermissions()) { @@ -176,4 +185,39 @@ public class PermissionsManager { this.server = server; } + public Collection<User> getUsers() { + return users.values(); + } + + public User getUser(long userId) { + return users.get(userId); + } + + public void addUser(User user) throws SQLException { + dataManager.addUser(user); + users.put(user.getId(), user); + refreshPermissions(); + } + + public void updateUser(User user) throws SQLException { + dataManager.updateUser(user); + users.put(user.getId(), user); + refreshPermissions(); + } + + public void removeUser(long userId) throws SQLException { + dataManager.removeUser(userId); + users.remove(userId); + refreshPermissions(); + } + + public User login(String email, String password) throws SQLException { + User user = dataManager.login(email, password); + if (user != null && users.get(user.getId()) != null) { + return users.get(user.getId()); + } else { + return null; + } + } + } |