Public class employee {private string ID; // employee ID: Private string name; // employee name: Private string Department; // Department: Private string phone; // The employee's contact number private int salary; // the employee's salary private string origin; // the source of the employee's information // construction method public employee (string ID) {This. id = ID; getdatafromlnfocenter () ;}// obtain the employee information in the database private void getdatafromlnfocenter () {// establish a connection well with the database to query the employee information, assign query results to name, department, plone, and SA Lary and other variables // assign the origin value to "from database "}......
Import Java. lang. ref. referencequeue; import Java. lang. ref. softreference; import Java. util. hashtable; public class employeecache {static private employeecache cache; // a cache instance private hashtable employeerefs; // Private referencequeue Q for storing cache content; // The spam reference queue // inherits softreference so that each instance has a recognizable identifier. // And the ID is the same as the key in the hashmap. Private class employeeref extends softreference {private string _ key = ""; Public employeeref (employee em, referencequeue q) {super (EM, q); _ key = em. GETID () ;}}// construct a cache instance private employeecache () {employeerefs = new hashtable (); q = new referencequeue ();} // obtain the public static employeecache getinstance () {If (Cache = NULL) {cache = new employeecache ();} return cache ;} // use soft references The instance of the employee object references and saves the reference private void cacheemployee (employee em) {cleancache (); // clear the spam reference employeeref ref = new employeeref (EM, q); employeerefs. put (EM. GETID (), ref);} // obtain the instance public employee getemployee (string ID) {employee em = NULL Based on the specified ID number; // check whether soft references of the employee instance exist in the cache. If so, obtain them from soft references. If (employeerefs. containskey (ID) {employeeref ref = (employeeref) employeerefs. get (ID); em = (employee) ref. get ();} // if there is no soft reference, or the Instance obtained from the soft reference is null, re-build an instance, // Save the soft reference if (Em = NULL) {em = new employee (ID); system. out. println ("retrieve from employeeinfocenter. id = "+ id); this. cacheemployee (EM);} return em;} // clear the private void cleancache () {employee Ref ref = NULL; while (ref = (employeeref) Q. Poll ())! = NULL) {employeerefs. remove (ref. _ key) ;}}// clear all content in the cache public void receivache () {cleancache (); employeerefs. clear (); system. GC (); system. runfinalization ();}}