In addition you can use the Cookie on the client, which can also use it as a data cache?

Issues such as the title, go straight. Introduce two other caches.

1.sessionStorage、localStorage 

localStorage:  means that you do not take the initiative to clear it, and it would have been in storage to the client, even if you close the client (browser), belonging to the local persistent storage memory data storage layer 

sessionStorage:  for a locally stored data session (session) is, once the session is closed, then the data will disappear, such as refreshing. 

Sometimes, we need to store the data sessionStorage and localStorage, the benefits of doing so are:
1 data cache
2 reduce memory usage
, however, storage can only store strings of data, JS commonly used for array or object can not direct storage.
It can hold more data (on IE8 is 10MB, Chrome is 5MB), while preserving the data will not be sent to the server, to avoid the waste of bandwidth.

localStorage storage method (similar to the sessionStorage) clear access as follows:

Deposit :
localStorage.name = 'Vanida;
localStorage [ "name"] =' Vanida ';
localStorage.setItem ( "name", "Vanida");
// set all three values are the same embodiment;

Take :
localStorage method of obtaining the value of
var name = localStorage [ "name"]
var name = localStorage.name
var name = localStorage.getItem ( "name");
// get the value of all three is the same way;

Clear :
localStorage method of clearing a certain value
// clear the value of name
localStorage.removeItem ( "name");
localStorage.name = ";
localStorage to Clear All methods
localStorage.clear ()

Json object-storage :
localStorage can only store strings, if the need to store objects, we must first be converted to a string. Using the JSON.stringify ();
var Person = {name: "Vanida", "Sex": "Girl", "Age": 25};
localStorage.setItem ( "Person", the JSON.stringify (Person));
// localStorage.person = "{" name ": " vanida "," sex ":" girl "," age ": 25}"
then you can remove the object person with the JSON.parse ();
person the JSON.parse = (localStorage .getItem ( "the Person"));
+++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++

Here is what I wrote cache.js: original local package:

//获取cookie的值
function getCookieVale(cookieObj, strCookie) {
    var cookieValu = "";
    strCookie = $.trim(strCookie);
    var arrCookie = cookieObj.split(";");
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split("=");
        if (strCookie == $.trim(arr[0])) {
            cookieValu = $.trim(arr[1]);
            break;
        }
    }
    return cookieValu;
}

//获取缓存信息
function getStory(itemCode) {
    return JSON.parse(sessionStorage.getItem(itemCode));
}
//删除缓存信息
function removeStory(itemCode) {
    sessionStorage.removeItem(itemCode);
}

//设置缓存信息
function setStory(itemCode, itemValu) {
    sessionStorage.setItem(itemCode, JSON.stringify(itemValu));
}
//清空session
function clearSessionStroy(){
    sessionStorage.clear();
}

function clearLoclStory(){
    localStorage.clear();
}

function getLoclStory(itemCode) {
    return JSON.parse(localStorage.getItem(itemCode));
}
//删除缓存信息
function removeLoclStory(itemCode) {
    localStorage.removeItem(itemCode);
}

//设置缓存信息
function setLoclStory(itemCode, itemValu) {
    localStorage.setItem(itemCode, JSON.stringify(itemValu));
}

 

Guess you like

Origin www.cnblogs.com/ysq0908/p/11455067.html