Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Titanium.Properties storage. #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
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_, ''));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also say:

if (p.indexOf(this.prefix_) === 0)

I think this is better since you don't have to keep the this.regexp_ variable around.

};
return ret;
}


/**
* Retrieves an item from the cache.
* @param {string} key The key to retrieve.
Expand Down