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 1 commit
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
34 changes: 34 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);
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_, ''));
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 Expand Up @@ -429,4 +461,6 @@ if (typeof module !== "undefined" && module.exports) {
root.Cache = Cache;
}

module.exports = Cache;
Copy link
Owner

Choose a reason for hiding this comment

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

This is already done on line 457, why are you doing it again here?

Copy link
Author

Choose a reason for hiding this comment

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

Was in a rush doing something else and didn't see it.
I'll add another PR without it. Please reject this one.


})();