WebserviceStore
| Title |
WebserviceStore : public : public |
| Author(s) |
LiXizhi |
| Date |
2008/2/28 |
| File |
script/kids/3DMapSystemApp/localserver/WebserviceStore.lua |
Description
A
WebserviceStore represents a set of web service response 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.
Sample Code
NPL.load("(gl)script/kids/3DMapSystemApp/localserver/WebserviceStore.lua");
Member Functions
WebserviceStore:CallRestEx
------------------------------------------
WebserviceStore : public <localserver>
------------------------------------------
local WebserviceStore = commonlib.inherit(Map3DSystem.localserver.ResourceStore, {
-- type of WebCacheDB.ServerType
server_type_ = WebCacheDB.ServerType.WEBSERVICE_STORE,
-- default policy
Cache_policy = Map3DSystem.localserver.CachePolicy:new("access plus 1 hour"),
Cache_policy_REST = Map3DSystem.localserver.CachePolicy:new("access plus 1 week"),
});
commonlib.setfield("Map3DSystem.localserver.WebserviceStore",
WebserviceStore);
functions:
call a HTML or other text page from the local server, using a local_cache_policy
If there is already a completed HTML pagebefore, we will return the response immediately. And then still proceed to do the update check if expired.
- param Cache :_policy: nil or default Cache_policy_REST. (one week time)
- param url : url of the HTML page
- param callbackFunc : the call back function(entry, callbackContext or url) end, entry.payload.cached_filepath is name of the file that contains the text returned via HTTP, usually it is xml, html, mcml, etc. so caller can construct an xml tree from it. Note that the callbackFunc may be called before this function returns since the request may be served locally.
- param callbackContext : this is an optional parameter that is passed to callbackFunc
- return return : true if it is fetching data or data is already available. it return nil or paraworld.errorcode, if web service can not be called at this time, due to error or too many concurrent calls.
syntax
function WebserviceStore:CallRestEx(Cache_policy, url, callbackFunc, callbackContext)
parameters
| Cache |
_policy: nil or default Cache_policy_REST. (one week time) |
| policy |
|
| url |
|
|
callbackFunc | the call back function(entry, callbackContext or url) end,
entry.payload.cached_filepath is name of the file that contains the text returned via HTTP, usually it is xml, html, mcml, etc. so caller can construct an xml tree from it.
Note that the callbackFunc may be called before this function returns since the request may be served locally. |
| callbackContext |
|
| return |
true if it is fetching data or data is already available. it return nil or paraworld.errorcode, if web service can not be called at this time, due to error or too many concurrent calls. |
WebserviceStore:CallXML
Same as
CallRestEx? . the only difference is that callbackFunc(xmlRootNode, entry, callbackContext) contains the xml table, instead of entry object.
syntax
function WebserviceStore:CallXML(Cache_policy, url, callbackFunc, callbackContext)
parameters
| Cache |
|
| policy |
|
| url |
|
| callbackFunc |
|
| callbackContext |
|
WebserviceStore:CallWebserviceEx
call a web service from the local server, using a local_cache_policy
If there is already a completed response before, we will return the response immediately. And then still proceed to do the update check if expired.
- param Cache :_policy: nil or Map3DSystem? .localserver.CachePolicy
- param url : url of the web service
- param msg : an NPL table to be sent.
- param REST :_policy: an array of variable names in the msg from which the web service request is converted to a REST style url. a REST style url encodes both the web services url and msg data. see UrlHelper for details.
- param callbackFunc : the call back function(msg, callbackContext or url) end, Note that the callbackFunc may be called before this function returns since the request may be served locally.
- param callbackContext : this is an optional parameter that is passed to callbackFunc
- return return : true if it is fetching data or data is already available.
syntax
function WebserviceStore:CallWebserviceEx(Cache_policy, url, msg, REST_policy, callbackFunc, callbackContext)
parameters
| Cache |
_policy: nil or Map3DSystem? .localserver.CachePolicy |
| policy |
|
| url |
|
| msg |
an NPL table to be sent. |
| REST |
|
| policy |
|
|
callbackFunc | the call back function(msg, callbackContext or url) end,
Note that the callbackFunc may be called before this function returns since the request may be served locally. |
| callbackContext |
|
| return |
true if it is fetching data or data is already available. |
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
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
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.
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
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. |