-
Notifications
You must be signed in to change notification settings - Fork 40
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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_, '')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -429,4 +461,6 @@ if (typeof module !== "undefined" && module.exports) { | |
root.Cache = Cache; | ||
} | ||
|
||
module.exports = Cache; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was in a rush doing something else and didn't see it. |
||
|
||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated link: http://blog.simonwillison.net/post/57956816139/escape