📄 servercachemanager.java
字号:
package net.s3o.server.cache;
import java.util.Iterator;
import java.util.Map;
import net.s3o.common.ClientInfoWrapper;
import net.s3o.common.S3OUtils;
import net.s3o.common.UserInfoWrapper;
import net.s3o.core.cache.S3OEhCacheFactory;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
public class ServerCacheManager implements InitializingBean, DisposableBean {
private S3OEhCacheFactory cacheFactory=null;
public static Cache clientCache = null;
public static Cache usersCacheByTicket = null;
public static Cache usersCacheByUserId = null;
public static Cache ticketCache = null;
public static long timeToIdle=1000;
public static long timeToLive=1000;
public void afterPropertiesSet() throws Exception {
Assert.notNull(cacheFactory,"属性 cacheFactory 没有指定.");
clientCache=cacheFactory.getCacheByName("clientCache");
usersCacheByTicket=cacheFactory.getCacheByName("usersCacheByTicket");
usersCacheByUserId=cacheFactory.getCacheByName("usersCacheByUserId");
ticketCache=cacheFactory.getCacheByName("ticketCache");
Assert.notNull(usersCacheByTicket,"缓存 usersCacheByTicket 没有在 cacheFactory 中指定.");
Assert.notNull(usersCacheByUserId,"缓存 usersCacheByUserId 没有在 cacheFactory 中指定.");
Assert.notNull(clientCache,"缓存 clientCache 没有在 cacheFactory 中指定.");
Assert.notNull(ticketCache,"缓存 ticketCache 没有在 cacheFactory 中指定.");
timeToIdle=usersCacheByTicket.getTimeToIdleSeconds();
timeToLive=usersCacheByTicket.getTimeToLiveSeconds();
}
public static String getTicketFromCacheByKey(String ticketKey){
Element elem=ticketCache.get(ticketKey);
String ticket=null;
if (elem!=null) {
ticket=(String)elem.getObjectValue();
}
return ticket;
}
public static String addTicketToCache(String ticket){
String key=null;
while (ticketCache.get(key)!=null){
key=S3OUtils.generateTicketKey(ticket);
}
ticketCache.put(new Element(key,ticket));
return key;
}
public static boolean removeTicket(String ticketKey){
return ticketCache.remove(ticketKey);
}
public static Map getClientInfo(String clientId){
Element elem=clientCache.get(clientId);
if (elem!=null) {
return (Map)elem.getObjectValue();
}
return null;
}
public static void setClientInfo(String clientId, Map clientInfo){
clientCache.put(new Element(clientId,clientInfo));
}
public static void clearClientCache(){
clientCache.removeAll();
}
public static int getNumberOfClients(){
return clientCache.getSize();
}
public static ClientInfoWrapper[] getAllClients(){
int size=clientCache.getSize();
Iterator keyI=clientCache.getKeys().iterator();
ClientInfoWrapper[] clientInfos=new ClientInfoWrapper[size];
int i=0;
Map clientInfoMap=null;
while (keyI.hasNext()){
clientInfoMap=(Map)clientCache.get(keyI.next()).getObjectValue();
if (clientInfoMap!=null){
clientInfos[i]=new ClientInfoWrapper(clientInfoMap);
i++;
}
}
return clientInfos;
}
public static String[] getAllClientsURL(){
int size=clientCache.getSize();
Iterator keyI=clientCache.getKeys().iterator();
String[] urls=new String[size];
int i=0;
Map clientInfo=null;
String url=null;
while (keyI.hasNext()){
clientInfo=(Map)clientCache.get(keyI.next()).getObjectValue();
url=(String)clientInfo.get(ClientInfoWrapper.KEY_OF_CLIENT_URL);
if (!S3OUtils.isEmpty(url)){
urls[i]=url;
i++;
}
}
return urls;
}
public static Map getUserInfoByTicket(String ticket){
Element elem=usersCacheByTicket.get(ticket);
if (elem!=null) {
return (Map)elem.getObjectValue();
}
return null;
}
public static Map getUserInfoByUserId(String userId){
Element elem=usersCacheByUserId.get(userId);
if (elem!=null) {
return (Map)elem.getObjectValue();
}
return null;
}
public static void setUserInfo(Map userInfoMap){
removeUserInfoByUserId((String)userInfoMap.get(UserInfoWrapper.KEY_OF_USER_ID));
setUserInfoByUserId(userInfoMap);
setUserInfoByTicket(userInfoMap);
}
public static boolean removeUserInfoByTicket(String ticket){
Map userInfo=getUserInfoByTicket(ticket);
if (userInfo!=null){
String userid=(String)userInfo.get(UserInfoWrapper.KEY_OF_USER_ID);
usersCacheByUserId.remove(userid);
}
return usersCacheByTicket.remove(ticket);
}
public static boolean removeUserInfoByUserId(String userId){
Map userInfo=getUserInfoByUserId(userId);
if (userInfo!=null){
String ticket=(String)userInfo.get(UserInfoWrapper.KEY_OF_USER_TICKET);
usersCacheByTicket.remove(ticket);
}
return usersCacheByUserId.remove(userId);
}
public static void clearUsersCache(){
clearUsersCacheByTicket();
clearUsersCacheByUserId();
}
/* ==================================== */
protected static void setUserInfoByTicket(Map userInfoMap){
String ticket=(String)userInfoMap.get(UserInfoWrapper.KEY_OF_USER_TICKET);
usersCacheByTicket.put(new Element(ticket,userInfoMap));
}
protected static void setUserInfoByUserId(Map userInfoMap){
String userid=(String)userInfoMap.get(UserInfoWrapper.KEY_OF_USER_ID);
usersCacheByUserId.put(new Element(userid,userInfoMap));
}
protected static void clearUsersCacheByTicket(){
usersCacheByTicket.removeAll();
}
protected static void clearUsersCacheByUserId(){
usersCacheByUserId.removeAll();
}
public S3OEhCacheFactory getCacheFactory() {
return cacheFactory;
}
public void setCacheFactory(S3OEhCacheFactory cacheFactory) {
this.cacheFactory = cacheFactory;
}
public void destroy() {
clientCache = null;
usersCacheByTicket = null;
usersCacheByUserId = null;
timeToIdle=1000;
timeToLive=1000;
cacheFactory.destroy();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -