⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachemanager.java

📁 旅游自助系统
💻 JAVA
字号:
/**
 * 
 */
package org.tshs.core;

import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.*;

import org.tshs.core.Constant.ObjectType;
import org.tshs.entity.Client;
import org.tshs.entity.SightSpot;
import org.tshs.entity.TravelCorp;
import org.tshs.entity.TravelDept;
import org.tshs.entity.TravelGroup;
import org.tshs.entity.TshsObject;
import org.tshs.exception.DBLockedException;
import org.tshs.storage.rdbms.DbManager;
import org.tshs.storage.rdbms.TableRow;
import org.tshs.storage.rdbms.TableRowIterator;

/**
 * Any TshsObject retrieving must use this class's methods.
 * On the purpose that each TshsObject found from database must be put into cache on first query.
 * 
 * @author Administrator
 *
 */
public class CacheManager {

	private static Map<Long, TshsObject> map = Collections.synchronizedMap(new HashMap<Long, TshsObject>());
	
	private static List<TshsObject> cacheList = Collections.synchronizedList(new LinkedList<TshsObject>());		
	
	private static int capacity = CfgManager.getIntProperty("cache.capacity");

	private static boolean locked = false;
	
	public static TshsObject retrieve(Long id) throws Exception{
		if(locked){
			throw new DBLockedException("Database is maintained now");
		}
		
		TshsObject obj = null;
		if(!map.containsKey(id)){				
			try {
				obj = getById(id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(obj == null){
				// id not exsist;
				return null;
			}
			cache(obj);	
		}
		return map.get(id);
	}
	
	/**
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	public static TshsObject getById(Long id) throws Exception{
		if(locked){
			throw new DBLockedException("Database is maintained now");
		}
		
		TableRow row = null;
		
		String tableName = null;
		
		if(id <= CfgManager.getLongProperty("traveldept.end")){			
			tableName = "traveldept";			
		}else if(id <= CfgManager.getLongProperty("sightspot.end")){			
			tableName = "sightspot";			
		}else if(id <= CfgManager.getLongProperty("travelcorp.end")){
			tableName = "travelcorp";			
		}else if(id <= CfgManager.getLongProperty("travelgroup.end")){			
			tableName = "travelgroup";			
		}else if(id <= CfgManager.getLongProperty("client.end")){			
			tableName = "client";			
		}else{
			// error;
			return null;
		}
		String sql = "select * from "+ tableName +" where id = '"+ id +"';";
		row = DbManager.querySingle(tableName, sql);
		
		return convert(row);
	}

	/**
	 * @param email
	 * @return
	 * @throws Exception 
	 */
	public static TshsObject getByEmail(String email) throws Exception{
		if(locked){
			throw new DBLockedException("Database is maintained now");
		}
		
		TableRow row = DbManager.queryByUnique("allemails", "email", email);
		if(row == null){
			return null;
		}
		String id = row.getColumn("id").toString();
		System.out.println(id);
		TshsObject obj = retrieve(Long.valueOf(id));
		
		return obj;
	}
	
	public static TshsObject getByUnique(ObjectType type, String column, String value) throws Exception {
		String table = null;
		if(type == ObjectType.CLIENT){
			table = "client";
		}else if(type == ObjectType.CORP){
			table = "travelcorp";
		}else if(type == ObjectType.DEPT){
			table = "traveldept";
		}else if(type == ObjectType.GROUP){
			table = "travelgroup";
		}else if(type == ObjectType.SIGHTSPOT){
			table = "sightspot";
		}
		
		return convert(DbManager.queryByUnique(table, column, value));
	}
	
	/**
	 * Convert database data structure "TableRow" to "TshsObject"
	 * 
	 * @param row
	 * @return
	 * @throws Exception 
	 */
	public static TshsObject convert(TableRow row) throws Exception{
		
		if(row == null){
			return null;
		}
		
		String tableName = row.getTableName();
		TshsObject obj = null;
		
		if("client".equals(Utils.toLowerCase(tableName))){			
			obj = new Client(row);			
		}else if("travelgroup".equals(Utils.toLowerCase(tableName))){			
			obj = new TravelGroup(row);			
		}else if("travelcorp".equals(Utils.toLowerCase(tableName))){			
			obj = new TravelCorp(row);			
		}else if("traveldept".equals(Utils.toLowerCase(tableName))){			
			obj = new TravelDept(row);			
		}else if("sightspot".equals(Utils.toLowerCase(tableName))){			
			obj = new SightSpot(row);			
		}else{
			String id = row.getColumn("id").toString();
			obj = retrieve(Long.valueOf(id));
		}
		
		cache(obj);
		return obj;
	}
	
	public static boolean contains(Long id){
		return map.containsValue(id);
	}

	/**
	 * @param obj
	 */
	private static void cache(TshsObject obj) {
		if(obj == null){
			// throw null cached exception
			return;
		}
		
		if(!map.containsKey(obj.getId())){
			checkCapacity();
			map.put(obj.getId(), obj);
			cacheList.add(0, obj);
		}
	}
	
	/**
	 * 
	 */
	private static void checkCapacity() {
		
		if(map.size() != capacity){
			return;
		}
		
		TshsObject temp = null;
		
		int removedNum = CfgManager.getIntProperty("cache.once.remove.num");
		if(removedNum == 0){
			removedNum = capacity/10;
		}
		
		for(int i = 0; i < removedNum; i++){
			temp = cacheList.remove(capacity - i - 1);
			map.remove(temp.getId());
		}
	}

	/*
	private static void remove(TshsObject obj){
		cacheList.remove(obj);
		map.remove(obj.getId());
	}*/
	
	public static void clearCache(){
		
		TshsObject temp = null;
		
		while(map.size() != 0){
			temp = cacheList.remove(0);
			map.remove(temp.getId());
		}
	}

	/**
	 * 
	 */
	public static void lockVisit(boolean toLock) {
		locked = toLock;
	}
	
	public static TableRowIterator getAllNames(ObjectType type) throws SQLException{
		String table = null;
		if(type == ObjectType.CLIENT){
			table = "client";
		}else if(type == ObjectType.CORP){
			table = "travelcorp";
		}else if(type == ObjectType.SIGHTSPOT){
			table = "sightspot";
		}else{
			System.out.println("Error from getAllData in CacheManager: invalid input ObjectType");
			return null;
		}
		
		String sql = "select name from " + table + ";";
		return DbManager.query(sql);
	}
	
	public static TableRowIterator getAllObj(ObjectType type) throws SQLException{
		String table = null;
		if(type == ObjectType.CLIENT){
			table = "client";
		}else if(type == ObjectType.CORP){
			table = "travelcorp";
		}else if(type == ObjectType.SIGHTSPOT){
			table = "sightspot";
		}else if(type == ObjectType.GROUP){
			table = "travelgroup";
		}else{
			System.out.println("Error from getAllData in CacheManager: invalid input ObjectType");
			return null;
		}
		
		String sql = "select * from " + table + ";";
		return DbManager.query(sql);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -