PEDN> Main Web>NPL>ResourceStore (2008-02-29, LiXizhi) Change language en zh-cn? EditAttach

ResourceStore

ResourceStore : public

Title ResourceStore : public
Author(s) LiXizhi
Date 2008/2/25
File script/kids/3DMapSystemApp/localserver/ResourceStore.lua

Description

A ResourceStore represents a set of entries in the WebCacheDB? and allows the set to be managed as a group. The identifying properties of a LocalServer are its domain, name, required_cookie, and server_type.

TIP Sample Code

NPL.load("(gl)script/kids/3DMapSystemApp/localserver/ResourceStore.lua");

Member Functions

ResourceStore.AppendHeader

------------------------------------------
 ResourceStore : public <localserver>
------------------------------------------
local ResourceStore = commonlib.inherit(Map3DSystem.localserver.localserver, {
   -- type of WebCacheDB.ServerType
   server_type_ = WebCacheDB.ServerType.RESOURCE_STORE,
   
   -- int64: The rowid of the version record in the DB associated with this
   -- ResourceStore. Each ResourceStore has exactly one related
   -- version record. The schema requires that Servers contain Versions
   -- contain Entries in order to support the other class of Server, the
   -- ManagedResourceStore. To satisfy the schema, this class manages
   -- the lifecyle of this record in the version table. When the Server
   -- record is inserted and delete, the Version record goes in lock step.
   version_id_;
   -- default policy
   Cache_policy = Map3DSystem.localserver.CachePolicy:new("access plus 1 day"),
}
);

commonlib.setfield("Map3DSystem.localserver.ResourceStore", ResourceStore);

private helper function

syntax

function ResourceStore.AppendHeader(headers, name, value) 

parameters

headers  
name  
value  

ResourceStore:GetFile


major API functions:

retrieve all url files using the local server.

  • param Cache :_policy: nil or Map3DSystem? .localserver.CachePolicy
  • param urls : url string or an array of urls. It will be retrieved one after another.
  • param callbackFunc : the call back function(entry, callbackContext or url) end will be called for each url in input urls.
  • param callbackContext : this is an optional parameter that is passed to callbackFunc where the entry is a table of {entry, payload, IsFromCache? }. Normally, one can access the disk file containing the reponse data by entry.payload.cached_filepath and other info by entry.payload:GetHeader(Map3DSystem? .localserver.HttpConstants.kXXX);
  • param callbackProgressFunc : this is an optional function that is called whenever a fraction of a url is downloaded the format is function(msg, callbackContext or url), where msg is {DownloadState=""|"complete"|"terminated", totalFileSize=number, currentFileSize=number, PercentDone? =number}
  • return return : true if it is fetching data or data is already available.

syntax

function ResourceStore:GetFile(Cache_policy, urls, callbackFunc, callbackContext, callbackProgressFunc) 

parameters

Cache _policy: nil or Map3DSystem? .localserver.CachePolicy
policy  
urls  
callbackFunc the call back function(entry, callbackContext or url) end will be called for each url in input urls.
callbackContext  
| callbackProgressFunc | this is an optional function that is called whenever a fraction of a url is downloaded the format is function(msg, callbackContext or url), where msg is {DownloadState=""|"complete"|"terminated", totalFileSize=number, currentFileSize=number, PercentDone? =number} |

ResourceStore:CreateOrOpen


member functions:
Initializes an instance and inserts rows in the Servers and Versions table of the DB if needed
  • return true : if succeed or nil if failed.

syntax

function ResourceStore:CreateOrOpen(security_origin, name, required_cookie) 

parameters

security  
origin  
name  
required  
cookie  
return if succeed or nil if failed.

ResourceStore:Open

Initializes an instance from its server_id. Will not insert rows into the Servers or Versions table of the DB. If the expected rows are not present in the Servers and Versions table, this method fails and returns false.

  • return nil : if failed. true if succeed.

syntax

function ResourceStore:Open(server_id)

parameters

server  
id  
return if failed. true if succeed.

ResourceStore:GetItem

Gets an item from the store including or not the response body

  • param info :_only: If info_only is true, the response body of it is not retrieved.
  • return the : localserver.Item {entry, payload}is returned or nil if not found.

syntax

function ResourceStore:GetItem(url, info_only)

parameters

url  
info _only: If info_only is true, the response body of it is not retrieved.
only  

ResourceStore:GetItemInfo

Gets an item from the store without retrieving the response body

syntax

function ResourceStore:GetItemInfo(url)

parameters

url  

ResourceStore:DeleteAll

Deletes all items in the store.

  • return __ : return true if succeed

syntax

function ResourceStore:DeleteAll() 

parameters

return return true if succeed

ResourceStore:Delete

Deletes a single item in the store.

  • return __ : return true if succeed

syntax

function ResourceStore:Delete(url) 

parameters

url  
return return true if succeed

ResourceStore:Rename

Renames an item in the store. If an item already exists for new_url, the pre-existing item is deleted.

  • return __ : return true if succeed

syntax

function ResourceStore:Rename(orig_url, new_url) 

parameters

orig  
url  
new  
url  
return return true if succeed

ResourceStore:Copy

Copies an item in the store. If an item already exists for dst_url, the pre-existing item is deleted.

syntax

function ResourceStore:Copy(src_url, dst_url)

parameters

src  
url  
dst  
url  

ResourceStore:IsCaptured

Returns true if an item is captured for url.

syntax

function ResourceStore:IsCaptured(url)

parameters

url  

ResourceStore:GetCapturedFileName

Returns the filename of a captured local file. return nil if failed, return empty string if not found.

syntax

function ResourceStore:GetCapturedFileName(url)

parameters

url  

ResourceStore:GetAllHeaders

Returns all http headers for url

syntax

function ResourceStore:GetAllHeaders(url) 

parameters

url  

creating instances of local servers

Title creating instances of local servers
Author(s) LiXizhi
Date 2008/2/27
File script/kids/3DMapSystemApp/localserver/factory.lua

Description

TIP Sample Code

NPL.load("(gl)script/kids/3DMapSystemApp/localserver/factory.lua");
local store = Map3DSystem.localserver.CreateStore("WebserviceStore_sample", 2)
local store = Map3DSystem.localserver.CreateStore("ResourceStore_sample", 1)
local store = Map3DSystem.localserver.CreateStore("ManagedResourceStore_sample", 0)

Member Functions

Map3DSystem.localserver.GetStore

get the loaded store. if the store is not loaded,it will return nil. To CreateGet? a store, use CreateStore? () instead.

syntax

function Map3DSystem.localserver.GetStore(name)

parameters

name  

cache policy

Title cache policy
Author(s) LiXizhi
Date 2008/3/2
File script/kids/3DMapSystemApp/localserver/cache_policy.lua

Description

In NPL local server, there is a single cache policy which is slight different from standard HTTP cache (The official version is here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9)

NPL local server caching policy

The cache policy by NPL local server (and represented by this CachePolicy class) is stated below:
  • find the entry in the local server. If a completed entry is found, it is always returned immediately, regardless of whether it has expired or not.
  • if no entry is found or the entry expired, a new entry will be fetched via remote server and updated in the local server.
  • When a entry is updated in the store, it will be returned to all callback functions registered to it.
Note1: It means that the same request might be returned twice. The first time is the local version at the time of call; and the second time is when the entry is just updated via the server. The second call is skipped if the server can verify that the content has not changed and only update the last access time in the local store. Note2: If the relative expire time is 0, the first call is ignored. Note3: If the relative expire time is larger than a year, the second call is always ignored. Note4: This same policy applies to ResourceStore, ManagedResourceStore, and WebserviceStore.For WebserviceStore, only access base time type is permitted. The server policy can be overwritten by local server policy, local server policy can be overwritten by per function policy

To increase efficiency, one can create several different cache policy objects and share them for all local stores and related function calls.

TIP Sample Code

NPL.load("(gl)script/kids/3DMapSystemApp/localserver/cache_policy.lua");
local cache_policy = Map3DSystem.localserver.CachePolicy:new("access plus 1 month");
cache_policy:IsExpired(ParaGlobal.GetSysDateTime()-80000);

-- there are some premade policies, which can be retrieve from Map3DSystem.localserver.CachePolicies, see below
local cp = Map3DSystem.localserver.CachePolicies["never"];
local cp = Map3DSystem.localserver.CachePolicies["always"];
local cp = Map3DSystem.localserver.CachePolicies["1 hour"];
local cp = Map3DSystem.localserver.CachePolicies["1 day"];

Member Functions

CachePolicy:new

--------------------------
 CachePolicy class
--------------------------

local CachePolicy = {
   -- The base time is either the last modification time of the file, or the time of the client's access to the document.
   -- 0 means that the file's last modification time should be used as the base time, 
   -- 1 means the client's access time should be used.
   -- nil Expiration is not enabled. 
   BaseTime = 1,
   -- Time in seconds to expire relative to BaseTime. e.g. 2592000 is a month, which is good for asset and images.  604800 is a week which is good for profile and content pages. 
   ExpireTime = 604800,
}
;

commonlib.setfield("Map3DSystem.localserver.CachePolicy", CachePolicy);

create the object and init from initCode. format of initCode, please see Init() function.

syntax

function CachePolicy:new(initCode)

parameters

initCode  

CachePolicy:Init

[[ Extacts the policy base time type expire time from an input string.

  • param initCode : string: it has the following syntax
" [plus] { }*" where is one of: access, now (equivalent to 'access'), modification The plus keyword is optional should be an integer value, and is one of: years months weeks days hours minutes seconds For example, any of the following can be used to make entries expire 1 month after being accessed: - "access plus 1 month" - "access plus 4 weeks" - "access plus 30 days" The expiry time can be fine-tuned by adding several ' ' clauses: - "access plus 1 month 15 days 2 hours" - "modification plus 5 hours 3 minutes"
  • return __ : Returns true if successful.
]]

syntax

function CachePolicy:Init(initCode)

parameters

| initCode | string: it has the following syntax " [plus] { }*" where is one of: access, now (equivalent to 'access'), modification The plus keyword is optional should be an integer value, and is one of: years months weeks days hours minutes seconds For example, any of the following can be used to make entries expire 1 month after being accessed: - "access plus 1 month" - "access plus 4 weeks" - "access plus 30 days" The expiry time can be fine-tuned by adding several ' ' clauses: - "access plus 1 month 15 days 2 hours" - "modification plus 5 hours 3 minutes" |

CachePolicy:IsExpired

whether time is expired.

  • param Basetime : the base time in second
  • return __ : true if input time is expired

syntax

function CachePolicy:IsExpired(Basetime)

parameters

Basetime the base time in second

CachePolicy:IsCacheEnabled

  • return whether : cache is enabled. i.e. self.ExpireTime is not 0.

syntax

function CachePolicy:IsCacheEnabled()

parameters

return cache is enabled. i.e. self.ExpireTime is not 0.

CachePolicy:IsCacheAlwaysUsed

  • return whether : cache is always used. i.e. self.ExpireTime over 1 year.

syntax

function CachePolicy:IsCacheAlwaysUsed()

parameters

return cache is always used. i.e. self.ExpireTime over 1 year.
Topic revision: r1 - 2008-02-29 - 15:26:12 - LiXizhi
 

ParaEngine Developer Network

This site is powered by the TWiki collaboration platformCopyright © 2004-2008 ParaEngine Corporation
Ideas, requests, problems regarding ParaEngine platform Send feedback