top

CAPH.DAL.LOCALSTORAGECACHE

LocalStorageCache is a class that provides caching functions. It uses HTML5 Local Storage. Main APIs of LocalStorageCache are 'find', 'insertBlob', 'insertBinary', 'insertText', 'get' and 'remove.' Using find method, a user can find an item with a key, which is usually a URL. Using insertBlob method, a user can insert a Blob into the cache. Using insertBinary method, a user can insert binary into the cache. Using insertText method, a user can insert text into the cache. Using get method, a user can get cached data. Using remove method, a user can remove a cache entry.


Constructor

LocalStorageCache

Description
Construct a LocalStorageCache
Emulator SupportY
SDK ConstraintNone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

Methods

encodeUInt8ArrayToBase64

Description
Encode a Uint8 TypedArray to a base64 string
Parameters
■input
  - Uint8 TypedArray
  - A Uint8 TypedArray as an input.
  - [default : null]
Return■String
  - A base64-encoded string
Emulator SupportY
SDK Constraintnone
Example
var BaseCache = caph.dal.BaseCache;
var basecache = new BaseCache();

// Let's say data  - - binary is given
var uInt8Array = new Uint8Array(binary);
var binaryData = basecache.encodeUInt8ArrayToBase64(uInt8Array);

queryMetadata

Description
Query metadata
Parameters
■url
  - String
  - url The key, URL
  - default : null
Return■Object
  - Metadata for the given URL.
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCach

var url = 'http://www.foo.com';

var metadata = queryMetadata(url);

LocalStorageCache

Description
(Constructor) Construct a LocalStorageCache
Parameters
■Void
Return■LocalStorageCache 
  - Instance of LocalStorageCache
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

remove

Description
Remove an item
Parameters
■url
  - String
  - the key. URL
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

// Let's say parameter - url is given
var removeFileName = localstoragecache.find(url);
localstoragecache.remove(removeFileName, function(){}, function (){} );

setBase64Proxy

Description
Set the Base64Encoder Proxy
Parameters
■proxy
  - Object
  - default value : null
  - A Base64Encoder Proxy
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

// Let's say the proxy is already given.

localstoragecache.setBase64Proxy(proxy);

insertBinary

Description
Insert item into localstorage as binary type
Parameters
■options
  - Object
   * url : the key
   * URL; blob : blob
   * size : the size of the encoded blob
■onSuccessCallback (Optional)
  - Function
  - callback on success.       
■onFailureCallback (Optional)
  - Function
  - callback on failure.
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

function onSuccessCallback() {
    console.log("insertBinary success");
}
function onFailureCallback() {
    console.log("insertBinary failure");
}

// Let's say data  - - url, binaryData, and size are given
var options = {};
options.url = url;                              
options.binary = binaryData;            
options.size = size;                          
localstoragecache.insertBinary(options, onSuccessCallback, onFailureCallback);

insertBlob

Description
Insert item into localstorage as blob type
Parameters
■options
  - Object
   * url : the key
   * URL; blob : blob
   * size : the size of the encoded blob
■onSuccessCallback  (Optional)
  - Function
  - callback on success.       
■onFailureCallback (Optional)
  - Function
  - callback on failure.
Return■Boolean
  - false, if fails; undefined, otherwise
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

function onSuccessCallback() {
    console.log("insertBlob success");
}
function onFailureCallback() {
    console.log("insertBlob failure");
}

// Let's say data  - - url, blobData, and size are given
var options = {};
options.url = url;                   
options.blob = blobData;       
options.size = size;                
localstoragecache.insertBlob(options, onSuccessCallback, onFailureCallback);

insertText

Description
Insert item into localstorage as text type
Parameters
■options
  - Object
   * url : the key
   * URL; blob : blob
   * size : the size of the encoded blob
■onSuccessCallback (Optional)
  - Function
  - callback on success.       
■onFailureCallback (Optional)
  - Function
  - callback on failure.
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

function onSuccessCallback() {
    console.log("insertText success");
}
function onFailureCallback() {
    console.log("insertText failure");
}

// Let's say data  - - url, text, and size are given
var options = {};
options.url = url;            
options.text = text;        
options.size = size;        
localstoragecache.insertText(options, onSuccessCallback, onFailureCallback);

find

Description
Find an item
Parameters
■url
  - String
  - the key. URL
Return■Boolean 
 - true, if it is found; false, otherwise
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

function successCallback () {
    console.log("insertBlob success");
}
function failureCallback() {
    console.log("insertBlob failure");
}
// Let's say data  - - url, blobData, and size are given
var option = {};
options.url = url;               
options.blob = blobData;    
options.size = size;            
localstoragecache.insertBlob(options, successCallback failureCallback);

var fileURL = option.url;
var result = localstorage.find(fileURL);              //return true

clearAll

Description
Clear all key/value pairs in the LocalStorageCache
Parameters
■Void
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

localstoragecache.clearAll();

init

Description
Initialize LocalStorageCache for caching
Parameters
■prefix
  - String 
  - prefix must be given.       
■sizeLimitInBytes 
  - Number
  - Size limit in bytes, localstoragecache's limit.       
■successCallback (Optional)
  - function 
  - this callback will be executed after initialize       loaclstorage       
■failureCallback (Optional)
    - function
    - Callback on failure
Return■Boolean
  - false, if fails; undefined, otherwise
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

localstoragecache.init("localstoragecache", 1024*1024*5/2, function() {
    console.log("success initializing localstoragecache");             
});

storeMeta

Description
Store metadata to a LocalStorage
Parameters
■Void
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

localstoragecache.storeMeta();

doLRU

Description
Remove some entries by using LRU algorithm
Parameters
■size
  - Number
  - A target size
  - [default : null]
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

localstoragecahe.doLRU(2 * 1024 * 1024);

getItemCount

Description
Returns item count
Parameters
■Void
Return■Number 
  - Number of Items
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var count = localstoragecache.getItemCount();

get

Description
Get the item. It is searched by given parameter, url
Parameters
■url
  - String
  - the key. URL
Return■Object
  - Object to return
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var item = localstoragecache.get(fileName);        
var img = document.createElement('img');
img.src = item;
document.body.appendChild(img);

buildItemKey

Description
Make a key for an item
Parameters
■key
  - String
  - A unique key of an item
  - [default : null]
Return■String 
  - A key for an item
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var keyString = localstoragecache.buildItemKey('http://www.foo.com');

buildMetaKey

Description
Build a key for the metadata record
Parameters
■Void
Return■String 
  - A key string for the metadata record
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var keyString = localstoragecache.buildMetaKey();

buildJSONFromMeta

Description
Build a JSON string From Metadata
Parameters
■meta
  - Object
  - A metadata
  - [default : null]
Return■String 
  - A JSON string represents a metadata item
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var meta = {};
meta._metaArray = [];
meta._metaArray.push('foo');
var jsonString = localstoragecache.buildJSONFromMeta(meta);

buildMetaFromJSON

Description
Build Metadata From a JSON string
Parameters
■meta
  - Object
  - A metadata
  - [default : null]
■json
  - String
  - A JSON for metadata
  - [default : null]
Return■Void
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var meta = {};
meta._metaArray = [];
var json = ['foo'];
localstoragecache.buildMetaFromJSON(meta, json);

getTotalSize

Description
Get the total item byte size
Parameters
■Void
Return■Number 
  - Total Item Byte Size of the LocalStorageCache
Emulator SupportY
SDK Constraintnone
Example
var LocalStorageCache = caph.dal.LocalStorageCache;
var localstoragecache = new LocalStorageCache();

var totalSize = localstoragecache.getTotalSize();