From a0ca7e07589e3332b8392e6f165503b1b9cf4e07 Mon Sep 17 00:00:00 2001 From: Marco Ferreira Date: Mon, 21 Oct 2013 01:13:33 +0200 Subject: [PATCH 1/2] Add support for Titanium.Properties storage. Now the module can be required (nodejs). --- cache.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cache.js b/cache.js index 3879302..d19ab6e 100644 --- a/cache.js +++ b/cache.js @@ -132,6 +132,38 @@ Cache.LocalStorageCacheStorage.prototype.keys = function() { return ret; } +// Titanium OS Properties Storage +Cache.TiStorage = function(namespace) { + this.prefix_ = 'cache-storage.' + (namespace || 'default') + '.'; + // Regexp String Escaping from http://simonwillison.net/2006/Jan/20/escape/#p-6 + var escapedPrefix = this.prefix_.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); + this.regexp_ = new RegExp('^' + escapedPrefix) +} +Cache.TiStorage.prototype.get = function(key) { + var item = Ti.App.Properties.getString(this.prefix_ + key, null); + if (item) return JSON.parse(item); + return null; +} +Cache.TiStorage.prototype.set = function(key, value) { + Ti.App.Properties.setString(this.prefix_ + key, JSON.stringify(value)); +} +Cache.TiStorage.prototype.size = function(key, value) { + return this.keys().length; +} +Cache.TiStorage.prototype.remove = function(key) { + var item = this.get(key); + delete Ti.App.Properties.removeProperty(this.prefix_ + key); + return item; +} +Cache.TiStorage.prototype.keys = function() { + var ret = [], p; + for (p in Ti.App.Properties.listProperties()) { + if (p.match(this.regexp_)) ret.push(p.replace(this.prefix_, '')); + }; + return ret; +} + + /** * Retrieves an item from the cache. * @param {string} key The key to retrieve. @@ -429,4 +461,6 @@ if (typeof module !== "undefined" && module.exports) { root.Cache = Cache; } +module.exports = Cache; + })(); From 2ba1c67cf0a0b3b051afd8ba469f9ce67a10b370 Mon Sep 17 00:00:00 2001 From: Marco Ferreira Date: Mon, 21 Oct 2013 10:10:26 +0200 Subject: [PATCH 2/2] Remove duplicate code and fix variable delete. --- cache.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cache.js b/cache.js index d19ab6e..b70be9d 100644 --- a/cache.js +++ b/cache.js @@ -152,7 +152,7 @@ Cache.TiStorage.prototype.size = function(key, value) { } Cache.TiStorage.prototype.remove = function(key) { var item = this.get(key); - delete Ti.App.Properties.removeProperty(this.prefix_ + key); + Ti.App.Properties.removeProperty(this.prefix_ + key); return item; } Cache.TiStorage.prototype.keys = function() { @@ -461,6 +461,4 @@ if (typeof module !== "undefined" && module.exports) { root.Cache = Cache; } -module.exports = Cache; - })();