Client side cache. Wrapper around indexedDB !!! Don't try to refactor this code - starting of transaction in separate promise is not work for Firefox!!! see this topic
Contain functions for simple key/value manipulations and advanced (entity related) manipulations.
Create separate database for each connection inside application.
For every database will create three store
- permanent for data persistent between sessions and
- session for data, live inside user session only (from login to login)
- user for custom data
var c = await new UBCache('mydb').createStorage();
c.put([
{key: 'note1', value: {prop1: 1, prop2: 'do something'} },
{key: 'note2', value: 'do something else'}
]).then();
c.get('note1').then(UB.logDebug); //output result to console
c.clear();
c.get('note1').then(function(value){
console.log(value === undefined ? 'all cleared': 'hm... something wrong')
})
# new UBCache (dbName: string, versionopt: number)
Arguments:
dbName
: stringName of indexedDB database we create. Usually this is UBConnection#baseURL. Constructor lower case dbName during creation
version
: numberOptionally database version
Members
# cacheTypes static
Possible cache types for business logic data
Name | Type | Description |
---|---|---|
None | string | No cache performed |
Entity | string | Client validate data version on server for each request |
Session | string | First request to data in the current session ALWAYS retrieve data from server. All other requests got a local copy |
SessionEntity | string | Client validate data version on server ONLY for first request in the current session |
# PERMANENT : string static
PERMANENT store name
# SESSION : string static
SESSION store name
# dbName instance
Name | Type | Description |
---|---|---|
dbName | string | name of indexedDB database |
Methods
# clear (storeNameopt: string) → Promise instance
Removes all data from the store
Arguments:
storeName
: stringdefault to 'userData'
# createStorage () → Promise.<UBCache> instance
If indexedDB is available and access to it is allowed - open an indexed DB, otherwise - fallback UBCache to use in-memory storage.
MUST be called and awaited before any get/put operations
# get (key: string, storeNameopt: string) → Promise instance
Retrieve data from store by key. If key not found - resolve result to undefined
Return:
resolved to key value.
# getAllKeys (storeNameopt: string) → Promise instance
Retrieves all values from store. This is slow operation - try to avoid it
Return:
resolved to Array of store keys
Arguments:
storeName
: stringdefault to 'userData'
# put (data: Object | Array.<{key: string, value}>, storeNameopt: string) → Promise instance
Put one or several values to store (in single transaction). Modifies existing values or inserts as new value if nonexistent.
If value === undefined
we put null instead, to understand in future get this is null value or key not exist
# remove (key: string | Array.<string> | RegExp, storeNameopt: string) → Promise instance
Remove data from store.
- If key is String - we delete one key;
- If key is Array - we delete all keys in array;
//remove data with key = 'key1' from userData store
$App.cache.remove('key1').then();
//remove 2 rows: with key = 'key1' and 'key2' from session store
$App.cache.remove(['key1', 'key2'], UBCache.SESSION).then();
# removeIfMach (regExp: RegExp, storeNameopt: string) → Promise instance
Remove data from store where keys match regExp.
Internally use UBCache#getAllKeys so is slow.
Better to use remove([key1, ..keyN])
console.time('removeIfMach')
$App.cache.removeIfMach(/^admin:ru:cdn_/, 'permanent').then(function() {
console.timeEnd('removeIfMach');
})