Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * pStorage
- * --------
- * a wrapper
- around
- PersistJS an
- CryptoJS to
- allow TTL and
- Content
- Validation
- * by Barry
- Shteiman, 2014,
- ref:www.
- sectorix.com
- */
- var pStorage =
- new function()
- {
- this.uid =
- 'my_
- pStorage';
- this.salt =
- '';
- this.
- datastore = new
- Persist.Store(
- this.uid);
- this.get =
- function get(
- key) {
- var
- entry = JSON.
- parse(this.
- datastore.get(
- key)||0);
- if (!
- entry) return
- null;
- if
- (entry.hash !==
- this.hash(
- entry.value +
- this.salt)) {
- this.datastore
- .remove(key);
- return null;
- }
- if
- (entry.ttl &
- ;& entry.
- ttl + entry.now
- < this.now
- ()) {
- this.datastore
- .remove(key);
- return null;
- }
- return entry.
- value;
- }
- this.set =
- function set(
- key,value,ttl)
- {
- this.
- datastore.set(
- key, JSON.
- stringify({
- ttl
- : ttl || 0,
- now
- : this.now()
- ,
- hash : this.
- hash(value +
- this.salt),
- value : value
- }) );
- }
- this.del =
- function del(
- key) {
- var
- entry = JSON.
- parse(this.
- datastore.get(
- key)||"0&
- quot;);
- if (!
- entry) return
- null;
- else {
- this.datastore.
- remove(key);
- return null;
- }
- }
- this.now =
- function now ()
- {return+new
- Date}
- this.hash =
- function hash(
- value) {return
- CryptoJS.MD5(
- value).toString
- ();}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement