Code snippet 3: Using Soft references to build a cache for sensitive data

Source: Internet
Author: User
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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.