|
| 1 | +JS-EasyCache |
| 2 | +=============================== |
| 3 | +This is a easy & fast library that emulates `memcache` functions using HTML5 `localStorage` with lossless data compression support, so that you can cache data on the client |
| 4 | +and associate an expiration time with each piece of data. If the `localStorage` limit (~5MB) is exceeded, it tries to create space by removing the items that are closest to expiring anyway. If `localStorage` is not available at all in the browser, the library degrades by simply not caching and all cache requests return null. |
| 5 | + |
| 6 | +JS-EasyCache uses high-performance lossless data compression algorithm to make data stored size smaller so you can store more data to the `localStorage`. |
| 7 | + |
| 8 | +Methods |
| 9 | +------- |
| 10 | + |
| 11 | +The library exposes 9 methods: `set()`, `get()`, `remove()`, `flush()`, `flushExpired()`, `setBucket()`, `resetBucket()`, `supported()`, and `enableWarnings()`. |
| 12 | + |
| 13 | +* * * |
| 14 | + |
| 15 | +### EasyCache.set |
| 16 | +Stores the value in localStorage. Expires after specified number of minutes. |
| 17 | +#### Arguments |
| 18 | +1. `key` (**string**) |
| 19 | +2. `value` (**Object|string**) |
| 20 | +3. `time` (**number: optional**) |
| 21 | + |
| 22 | +* * * |
| 23 | + |
| 24 | +### EasyCache.get |
| 25 | +Retrieves specified value from localStorage, if not expired. |
| 26 | +#### Arguments |
| 27 | +1. `key` (**string**) |
| 28 | + |
| 29 | +#### Returns |
| 30 | +**string | Object** : The stored value. If no value is available, null is returned. |
| 31 | + |
| 32 | +* * * |
| 33 | + |
| 34 | +### EasyCache.remove |
| 35 | +Removes a value from localStorage. |
| 36 | +#### Arguments |
| 37 | +1. `key` (**string**) |
| 38 | + |
| 39 | +* * * |
| 40 | + |
| 41 | +### EasyCache.flush |
| 42 | +Removes all EasyCache items from localStorage without affecting other data. |
| 43 | + |
| 44 | +* * * |
| 45 | + |
| 46 | +### EasyCache.setBucket |
| 47 | +Appends CACHE_PREFIX so EasyCache will partition data in to different buckets |
| 48 | +#### Arguments |
| 49 | +1. `bucket` (**string**) |
| 50 | + |
| 51 | +Usage |
| 52 | +------- |
| 53 | + |
| 54 | +The interface should be familiar to those of you who have used `memcache`, and should be easy to understand for those of you who haven't. |
| 55 | + |
| 56 | +For example, you can store a string for 2 minutes using `EasyCache.set()`: |
| 57 | + |
| 58 | +```js |
| 59 | +EasyCache.set('greeting', 'Hello World!', 2); |
| 60 | +``` |
| 61 | + |
| 62 | +You can then retrieve that string with `EasyCache.get()`: |
| 63 | + |
| 64 | +```js |
| 65 | +alert(EasyCache.get('greeting')); |
| 66 | +``` |
| 67 | + |
| 68 | +You can remove that string from the cache entirely with `EasyCache.remove()`: |
| 69 | + |
| 70 | +```js |
| 71 | +EasyCache.remove('greeting'); |
| 72 | +``` |
| 73 | + |
| 74 | +You can remove all items from the cache entirely with `EasyCache.flush()`: |
| 75 | + |
| 76 | +```js |
| 77 | +EasyCache.flush(); |
| 78 | +``` |
| 79 | + |
| 80 | +You can remove only expired items from the cache entirely with `EasyCache.flushExpired()`: |
| 81 | + |
| 82 | +```js |
| 83 | +EasyCache.flushExpired(); |
| 84 | +``` |
| 85 | + |
| 86 | +Returns whether local storage is supported.: |
| 87 | + |
| 88 | +```js |
| 89 | +alert(EasyCache.supported()); |
| 90 | +``` |
| 91 | + |
| 92 | +The library also takes care of serializing objects, so you can store more complex data: |
| 93 | + |
| 94 | +```js |
| 95 | +EasyCache.set('data', {'name': 'Hemn', 'age': 26}, 2); |
| 96 | +``` |
| 97 | + |
| 98 | +And then when you retrieve it, you will get it back as an object: |
| 99 | + |
| 100 | +```js |
| 101 | +alert(EasyCache.get('data').name); |
| 102 | +``` |
| 103 | + |
| 104 | +If you have multiple instances of EasyCache running on the same domain, you can partition data in a certain bucket via: |
| 105 | + |
| 106 | +```js |
| 107 | +EasyCache.set('response', '...', 2); |
| 108 | +EasyCache.setBucket('lib'); |
| 109 | +EasyCache.set('path', '...', 2); |
| 110 | +EasyCache.flush(); // Only removes 'path' which was set in the lib bucket |
| 111 | +EasyCache.resetBucket(); |
| 112 | +EasyCache.flush(); // Remove all EasyCache items and expiry markers without affecting rest of localStorage |
| 113 | +``` |
| 114 | + |
| 115 | +For more live examples, play around with the demo here: |
| 116 | +http://iprodev.github.io/JS-EasyCache/demo/index.html |
| 117 | + |
| 118 | + |
| 119 | +Real-World Usage |
| 120 | +---------- |
| 121 | +This library was originally developed with the use case of caching results of JSON API queries |
| 122 | +to speed up my webapps and give them better protection against flaky APIs. |
| 123 | + |
| 124 | +For example use `EasyCache` to fetch Youtube API results for 10 minutes: |
| 125 | + |
| 126 | +```js |
| 127 | +var key = 'youtube:' + query; |
| 128 | +var json = EasyCache.get(key); |
| 129 | +if (json) { |
| 130 | + processJSON(json); |
| 131 | +} else { |
| 132 | + fetchJSON(query); |
| 133 | +} |
| 134 | + |
| 135 | +function processJSON(json) { |
| 136 | + // .. |
| 137 | +} |
| 138 | + |
| 139 | +function fetchJSON() { |
| 140 | + var searchUrl = 'http://gdata.youtube.com/feeds/api/videos'; |
| 141 | + var params = { |
| 142 | + 'v': '2', 'alt': 'jsonc', 'q': encodeURIComponent(query) |
| 143 | + } |
| 144 | + JSONP.get(searchUrl, params, null, function(json) { |
| 145 | + processJSON(json); |
| 146 | + EasyCache.set(key, json, 10); |
| 147 | + }); |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +It does not have to be used for only expiration-based caching, however. It can also be used as just a wrapper for `localStorage`, as it provides the benefit of handling JS object (de-)serialization. |
| 152 | + |
| 153 | +Browser Support |
| 154 | +---------------- |
| 155 | + |
| 156 | +The `EasyCache` library should work in all browsers where `localStorage` is supported. |
| 157 | +A list of those is here: |
| 158 | +http://caniuse.com/#search=localstorage |
| 159 | + |
| 160 | +## Credits |
| 161 | + |
| 162 | +JS-EasyCache was created by [Hemn Chawroka](http://iprodev.com) from [iProDev](http://iprodev.com). Released under the MIT license. |
0 commit comments