Cache in JavaScript

Source: Internet
Author: User

<! DOCTYPE html>//Requirements:        //to implement a function to create a cache: Createcache ()        //1 reading the cache        //2 Adding a cache        //3 Modifying the cache        //4 The number of caches to be controlled within a certain range        //5 each call to create a cached function creates a cache .        //and multiple caches do not affect each other!         //Configuration properties or configuration files        varCachelength = 3; //Analysis:        functionCreatecache () {varCache = {}; //function: Used to record the order of keys stored in the cache            varKeyarr = []; //role: To increase and revise the cache to check            return function(key, value) {//1 Number of parameters to be processed first                //if it is a parameter: indicates a read                //if it is two parameters: indicates a setting                //A. Value = = undefined                //B. Arguments.length                if(Value = = =undefined) {                    returnCache[key]; }                //The following code handles setting up the cache                //1 Modifications                //2 Add                //Cache[key] = = = Undefined                //If the judgment is: true, the description is added                //Otherwise, it is modified                if(Cache[key]!==undefined) {Cache[key]=value; } Else {                    //The following code handles the added logic                    //1 The length of the cache exceeds the limit                    //2 The length of the cache does not exceed the limit                    //arr.length >                    //                         //If you first add data to the data, you can judge the value of the:> limit.                    //if it is the data that is added after judgment, it is: The value of >= limitKeyarr.push (key); if(Keyarr.length >cachelength) {                        //Delete, question: Which piece of data to delete                        //Delete: The data that was first added in the array                        //That is: The value of index number: 0                        varDeleteKey =Keyarr.shift (); DeleteCache[deletekey]; }                    //add data to the cache regardless of whether the data is deleted or not deletedCache[key] =value;        }            }; }        //lvha = LV Hao        //: Linked        //: Visited        //: Hover        //: Active        //called        varTypecache =Createcache (); //To Add a cache:Typecache ("Class", ". CLS"); Typecache ("id", "#dv"); Typecache ("Tag", "Div, P"); Typecache ("Tag", "Span"); Typecache (": Even", "dddd"); //read:Console.log (Typecache ("class"));//. CLSConsole.log (Typecache ("id"));//#dvConsole.log (Typecache ("tag"));//spanConsole.log (Typecache (": even"));//span        varStrcache =Createcache (); </script> <script type= "Text/javascript" >varCachelength = 3; //Optimized Code        functionCreatecache () {//function: Used to record the order of keys stored in the cache            varCache = {},            //role: To increase and revise the cache to checkKeyarr = []; return function(key, value) {if(Value = = =undefined) {                    returnCache[key]; }                if(Cache[key] = = =undefined) {                    //return value of the Push method: length after adding data                    if(Keyarr.push (key) >cachelength) {                        Deletecache[Keyarr.shift ()]; }} Cache[key]=value;        }; }        //called        varTypecache =Createcache (); //To Add a cache:Typecache ("Class", ". CLS"); Typecache ("id", "#dv"); Typecache ("Tag", "Div, P"); Typecache ("Tag", "Span"); Typecache (": Even", "dddd"); //read:Console.log (Typecache ("class"));//undefinedConsole.log (Typecache ("id"));//#dvConsole.log (Typecache ("tag"));//spanConsole.log (Typecache (": even"));//dddd</script></body>

Cache in JavaScript

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: info-contact@alibabacloud.com 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.