Skip to content

Instantly share code, notes, and snippets.

@justingreerbbi
Created November 11, 2021 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justingreerbbi/b8147d69d9494cda1c0de908fae67691 to your computer and use it in GitHub Desktop.
Save justingreerbbi/b8147d69d9494cda1c0de908fae67691 to your computer and use it in GitHub Desktop.
Titanium Module Cache CommonJS module - Plug n Play - Easy Cache
/***************************************************
Titanium Cache Module
Modified By: Justin Greer
https://justin-greer.com
Original Author: Joe Maffia
http://about.me/joemaffia
A cache module to be used for Titanium app. http://www.appcelerator.com/
It uses the local SQLite database to cache strings and JavaScript objects.
Usage:
Place this file (cache.js) in the project "lib" folder. You must create this folder if it does not exist.
Load the module
var cache = require('cache');
cache.put('key', 'value', 3600);
cache.get('key');
cache.del('key');
......
***************************************************/
var Cache = function() {}
// Cache initialization
Cache.prototype.initCache = function(cache_expiration_interval) {
var db = Titanium.Database.open('cache');
db.execute('CREATE TABLE IF NOT EXISTS cache (key TEXT UNIQUE, value TEXT, expiration INTEGER)');
db.close();
Ti.API.info('[CACHE] INITIALIZED');
};
Cache.prototype.expireCache = function() {
var db = Titanium.Database.open('cache');
var timestamp = currentTimestamp();
// count how many objects will be deleted
var count = 0;
var rs = db.execute('SELECT COUNT(*) FROM cache WHERE expiration <= ?', timestamp);
while (rs.isValidRow()) {
count = rs.field(0);
rs.next();
}
rs.close();
// deletes everything older than timestamp
db.execute('DELETE FROM cache WHERE expiration <= ?', timestamp);
db.close();
Ti.API.debug('[CACHE] EXPIRATION: [' + count + '] object(s) expired');
};
Cache.prototype.currentTimestamp = function() {
var value = Math.floor(new Date().getTime() / 1000);
Ti.API.debug("[CACHE] currentTimestamp=" + value);
return value;
};
Cache.prototype.get = function(key) {
var db = Titanium.Database.open('cache');
var rs = db.execute('SELECT value FROM cache WHERE key = ?', key);
var result = null;
if (rs.isValidRow()) {
Ti.API.info('[CACHE] GET: key[' + key + ']');
result = JSON.parse(rs.fieldByName('value'));
} else {
Ti.API.info('[CACHE] Missed: key[' + key + ']');
}
rs.close();
db.close();
return result;
};
Cache.prototype.put = function(key, value, expiration_seconds) {
if (!expiration_seconds) {
expiration_seconds = 300;
}
var expires_in = currentTimestamp() + expiration_seconds;
var db = Titanium.Database.open('cache');
Ti.API.info('[CACHE] PUT: time=' + currentTimestamp() + ', expires_in=' + expires_in);
var query = 'INSERT OR REPLACE INTO cache (key, value, expiration) VALUES (?, ?, ?);';
db.execute(query, key, JSON.stringify(value), expires_in);
db.close();
};
Cache.prototype.del = function(key) {
var db = Titanium.Database.open('cache');
db.execute('DELETE FROM cache WHERE key = ?', key);
db.close();
Ti.API.info('[CACHE] DEL: key[' + key + ']');
};
module.exports = new Cache;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment