Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SessionStorage cache library
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
You can’t perform that action at this time.