ManagedResourceStore
Title |
ManagedResourceStore : public |
Author(s) |
LiXizhi |
Date |
2008/2/25 |
File |
script/kids/3DMapSystemApp/localserver/ManagedResourceStore.lua |
Description
A
ManagedResourceStore 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.
Sample Code
NPL.load("(gl)script/kids/3DMapSystemApp/localserver/ManagedResourceStore .lua");
Member Functions
ManagedResourceStore:new
------------------------------------------
ManagedResourceStore : public <localserver>
------------------------------------------
local ManagedResourceStore = commonlib.inherit(Map3DSystem.localserver.localserver, {
-- type of WebCacheDB.ServerType
server_type_ = WebCacheDB.ServerType.MANAGED_RESOURCE_STORE,
-- default policy
Cache_policy = Map3DSystem.localserver.CachePolicy:new("access plus 1 month"),
}
);
commonlib.setfield("Map3DSystem.localserver.ManagedResourceStore",
ManagedResourceStore);
syntax
function ManagedResourceStore:new(o)
parameters
ManagedResourceStore:CreateOrOpen
public member functions:
Initializes an instance and inserts rows in the Servers and Versions table of the DB if needed
syntax
function ManagedResourceStore:CreateOrOpen(security_origin, name, required_cookie)
parameters
security |
|
origin |
|
name |
|
required |
|
cookie |
|
ManagedResourceStore: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.
syntax
function ManagedResourceStore:Open(server_id)
parameters
ManagedResourceStore:GetManifestUrl
Get the manifest url for this store
- param manifest :_url: string
- return __ : the manifest url (maybe ""), it returns nil if failed.
syntax
function ManagedResourceStore:GetManifestUrl(manifest_url)
parameters
manifest |
_url: string |
url |
|
ManagedResourceStore:SetManifestUrl
Sets the manifest url for this store
- param manifest :_url: string
syntax
function ManagedResourceStore:SetManifestUrl(manifest_url)
parameters
manifest |
_url: string |
url |
|
ManagedResourceStore:HasVersion
returns if the application has a version in the desired state or a version string.
syntax
function ManagedResourceStore:HasVersion(state)
parameters
ManagedResourceStore:GetUpdateInfo
Retrieves the update info for this store
- param return : status, last_time, manifest_date_header, update_error
syntax
function ManagedResourceStore:GetUpdateInfo()
ManagedResourceStore:SetUpdateInfo
Sets the update info for this applicaiton
syntax
function ManagedResourceStore:SetUpdateInfo(status, last_time, manifest_date_header, update_error)
parameters
status |
|
last |
|
time |
|
manifest |
|
date |
|
header |
|
update |
|
error |
|
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. |