-
-
Save phillipharding/23452ebe6ffebacb6ec510df56d005a6 to your computer and use it in GitHub Desktop.
SessionStorage cache library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CC = CC || {}; | |
CC.CORE = CC.CORE || {}; | |
CC.CORE.Cache = (function () { | |
var defaultCacheExpiry = 15 * 60 * 1000; // default is 15 minutes | |
var aMinuteInMs = (1000 * 60); | |
var anHourInMs = aMinuteInMs * 60; | |
var getCacheObject = function () { | |
// Using session storage rather than local storage as caching benefit | |
// is minimal so would rather have an easy way to reset it. | |
return window.sessionStorage; | |
}; | |
var isSupportStorage = function () { | |
var cacheObj = getCacheObject(); | |
var supportsStorage = cacheObj && JSON && typeof JSON.parse === "function" && typeof JSON.stringify === "function"; | |
if (supportsStorage) { | |
// Check for dodgy behaviour from iOS Safari in private browsing mode | |
try { | |
var testKey = "candc-cache-isSupportStorage-testKey"; | |
cacheObj[testKey] = "1"; | |
cacheObj.removeItem(testKey); | |
return true; | |
} | |
catch (ex) { | |
// Private browsing mode in iOS Safari, or possible full cache | |
} | |
} | |
CC.CORE.Log("Browser does not support caching"); | |
return false; | |
}; | |
var getExpiryKey = function (key) { | |
return key + "_expiry"; | |
}; | |
var isCacheExpired = function (key) { | |
var cacheExpiryString = getCacheObject()[getExpiryKey(key)]; | |
if (typeof cacheExpiryString === "string" && cacheExpiryString.length > 0) { | |
var cacheExpiryInt = parseInt(cacheExpiryString); | |
if (cacheExpiryInt > (new Date()).getTime()) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
var get = function (key) { | |
if (isSupportStorage()) { | |
if (!isCacheExpired(key)) { | |
var valueString = getCacheObject()[key]; | |
if (typeof valueString === "string") { | |
CC.CORE.Log("Got from cache at key: " + key); | |
if (valueString.indexOf("{") === 0 || valueString.indexOf("[") === 0) { | |
var valueObj = JSON.parse(valueString); | |
return valueObj; | |
} | |
else { | |
return valueString; | |
} | |
} | |
} | |
else { | |
// remove expired entries? | |
// not required as we will almost always be refreshing the cache | |
// at this time | |
} | |
} | |
return null; | |
}; | |
var set = function (key, valueObj, validityPeriodMs) { | |
var didSetInCache = false; | |
if (isSupportStorage()) { | |
// Get value as a string | |
var cacheValue = undefined; | |
if (valueObj === null || valueObj === undefined) { | |
cacheValue = null; | |
} | |
else if (typeof valueObj === "object") { | |
cacheValue = JSON.stringify(valueObj); | |
} | |
else if (typeof valueObj.toString === "function") { | |
cacheValue = valueObj.toString(); | |
} | |
else { | |
alert("Cannot cache type: " + typeof valueObj); | |
} | |
// Cache value if it is valid | |
if (cacheValue !== undefined) { | |
// Cache value | |
getCacheObject()[key] = cacheValue; | |
// Ensure valid expiry period | |
if (typeof validityPeriodMs !== "number" || validityPeriodMs < 1) { | |
validityPeriodMs = defaultCacheExpiry; | |
} | |
// Cache expiry | |
getCacheObject()[getExpiryKey(key)] = ((new Date()).getTime() + validityPeriodMs).toString(); | |
CC.CORE.Log("Set in cache at key: " + key); | |
didSetInCache = true; | |
} | |
} | |
return didSetInCache; | |
}; | |
var clear = function (key) { | |
var cache = getCacheObject(); | |
cache.removeItem(key); | |
cache.removeItem(getExpiryKey(key)); | |
}; | |
return { | |
Get: get, | |
Set: set, | |
Clear: clear, | |
IsSupportStorage: isSupportStorage, | |
Timeout: { | |
VeryShort: (aMinuteInMs * 1), | |
Default: (anHourInMs * 2), | |
VeryLong: (anHourInMs * 72), | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment