ASP Cache class _asp class classes

Source: Internet
Author: User
Tags datetime delete cache
As for the role of caching, I guess I don't have to say any more, its role has been very obvious, especially for the information is very large or full database pages of the site, he can make good use of the memory resources of the host, speed up the implementation of ASP efficiency, reduce the burden on the server, and the dynamic network in this respect to do is the most prominent, Like his current version of the dvbbs7.1.0, but also in the use of the cache to a higher level, before and after most of the operation is related to the cache, and now move the net is used is the fan of the prodigal son of the cache class, the following list of the three major experts to write ASP cache class

It's written by a wooden bird.
Copy Code code as follows:

'**********************************************
' VBS CACHE Class

' attribute valid, available, judged before value
' attribute Name,cache name, assign value after new object
' Method Add (value, expiration time), set cache content
' Property value, return cache content
' Property blempty, whether the value is not set
' Method Makeempty, free memory, test with
' Method equal (variable 1) to determine if the cache value is the same as the variable 1
' Method expires (time) to modify the expiration times
' Wooden bird 2002.12.24
' http://www.aspsky.net/
'**********************************************
Class Cache
Private obj ' cache content
Private Expiretime ' Expiration time
Private Expiretimename ' Expired time application name
Private CacheName ' cache content application name
Private path ' URI

Private Sub Class_Initialize ()
Path=request.servervariables ("url")
Path=left (Path,instrrev (Path, "/")
End Sub

Private Sub Class_Terminate ()
End Sub

Public Property Get Blempty
' Is empty
If IsEmpty (obj) Then
Blempty=true
Else
Blempty=false
End If
End Property

Public Property Get Valid
' is available (expired)
If IsEmpty (obj) or not isDate (expiretime) Then
Valid=false
ElseIf CDate (expiretime) <now Then
Valid=false
Else
Valid=true
End If
End Property

Public Property Let name (str)
' Set cache name
CACHENAME=STR & Path
Obj=application (CacheName)
EXPIRETIMENAME=STR & "Expires" & Path
Expiretime=application (Expiretimename)
End Property

Public Property Let expires (tm)
' Reset Expiration Time
Expiretime=tm
Application.Lock
Application (Expiretimename) =expiretime
Application.UnLock
End Property

Public Sub Add (Var,expire)
' Assign value
If IsEmpty (Var) or not isDate (expire) Then
Exit Sub
End If
Obj=var
Expiretime=expire
Application.Lock
Application (CacheName) =obj
Application (Expiretimename) =expiretime
Application.UnLock
End Sub

Public Property Get Value
' Take value
If IsEmpty (obj) or not isDate (expiretime) Then
Value=null
ElseIf CDate (expiretime) <now Then
Value=null
Else
Value=obj
End If
End Property

Public Sub Makeempty ()
' Release application
Application.Lock
Application (CacheName) =empty
Application (Expiretimename) =empty
Application.UnLock
Obj=empty
Expiretime=empty
End Sub

Public function equal (VAR2)
' Comparison
If TypeName (obj) <>typename (var2) Then
Equal=false
ElseIf TypeName (obj) = "Object" Then
If obj is var2 then
Equal=true
Else
Equal=false
End If
ElseIf TypeName (obj) = "Variant ()" Then
If join (obj, "^") =join (var2, "^") Then
Equal=true
Else
Equal=false
End If
Else
If Obj=var2 Then
Equal=true
Else
Equal=false
End If
End If
End Function
End Class
Wood Birds example VBS cache class

' attribute valid, available, judged before value
' attribute Name,cache name, assign value after new object
' Method Add (value, expiration time), set cache content
' Property value, return cache content
' Property blempty, whether the value is not set
' Method Makeempty, free memory,
' Method Delcahe, delete memory
' Method equal (variable 1) to determine if the cache value is the same as the variable 1
' Method expires (time) to modify the expiration times
' Usage

Set Mycache=new Cache
Mycache.name= "Boardjumplist" defines the cache name
If Mycache.valid then ' determines whether it is available (including expiration, and whether it is a null value)
Response.Write Mycache.value ' Output
Else
................
Boardjumplist=xxx
Mycache.add Boardjumplist,dateadd ("n", 60,now) ' Write cache xxx.add content, expiration time
Response.Write boardjumplist ' Output
End If
Mycache.makeempty () Free memory
Mycache. Delcahe () Delete cache

The prodigal son of the city wrote
Copy Code code as follows:

Class Cls_cache
Rem ================== Use Instructions ====================
Rem = This class module is the pioneer of the dynamic network, the author: The Prodigal son of the city. If you use this class module, please do not remove this description. This comment does not affect the speed of execution.
Rem = function: Caching and caching management classes
Rem = public variable: reloadtime expiration (in minutes) defaults to 14400
Rem = Maxcount The maximum value of the cached object, exceeding automatically deleting objects that are less frequently used. The default value is 300
Rem = The total name of the CacheName cache group, the default value is "Dvbbs", and if there is more than one cache group in a site, you need to change this value externally.
Rem = attribute: Name defines cache object names, write-only properties.
Rem = attribute: Value reads and writes cached data.
Rem = function: Objisempty () Determines whether the current cache expires.
Rem = method: Delcahe (Mycahename) Deletes a cached object manually, and the parameter is the name of the cached object.
Rem ========================
Public Reloadtime,maxcount,cachename
Private Localcachename,cachedata,delcount
Private Sub Class_Initialize ()
reloadtime=14400
Cachename= "Dvbbs"
End Sub
Private Sub Setcache (setname,newvalue)
Application.Lock
Application (setname) = NewValue
Application.UnLock
End Sub
Private Sub Makeempty (setname)
Application.Lock
Application (setname) = Empty
Application.UnLock
End Sub
Public Property Let Name (ByVal vnewvalue)
Localcachename=lcase (Vnewvalue)
End Property
Public Property Let Value (ByVal vnewvalue)
If localcachename<> "" Then
Cachedata=application (cachename& "_" &localcachename)
If IsArray (cachedata) Then
CacheData (0) =vnewvalue
CacheData (1) =now ()
Else
ReDim CacheData (2)
CacheData (0) =vnewvalue
CacheData (1) =now ()
End If
Setcache cachename& "_" &localcachename,cachedata
Else
Err.Raise vbObjectError + 1, "Dvbbscacheserver", "Please change the cachename."
End If
End Property
Public Property Get Value ()
If localcachename<> "" Then
Cachedata=application (cachename& "_" &localcachename)
If IsArray (cachedata) Then
Value=cachedata (0)
Else
Err.Raise vbObjectError + 1, "Dvbbscacheserver", "the CacheData is Empty."
End If
Else
Err.Raise vbObjectError + 1, "Dvbbscacheserver", "Please change the cachename."
End If
End Property
Public Function Objisempty ()
Objisempty=true
Cachedata=application (cachename& "_" &localcachename)
If not IsArray (cachedata) Then Exit Function
If not IsDate (CacheData (1)) Then Exit Function
If DateDiff ("s", CDate (CacheData (1)), now ()) < 60*reloadtime Then
Objisempty=false
End If
End Function
Public Sub Delcahe (mycahename)
Makeempty (cachename& "_" &mycahename)
End Sub
End Class
The example of prodigal son in the city
Set wydcache=new Cls_cache
wydcache.reloadtime=0.5 ' defines expiration time (minutes per meeting)
Wydcache.cachename= "Pages" ' Define cache name
If Wydcache.objisempty () Then ' Determines whether it is available (including expiration, and whether it is a null value)
Response.Write Wydcache.value
Else
..................
Boardjumplist=xxx
Wydcache.value=boardjumplist ' Write content
Response.Write Boardjumplist
End If

Mycache. Delcahe ("cache name") Delete cache

Slightboy wrote ' ========================
Copy Code code as follows:

' Clscache.asp
' ========================
' = = begin:2004-6-26 21:51:47
' = = Copyright:slightboy (C) 1998-2004
' = = email:slightboy@msn.com
' ========================
' ========================
' Dim Application (2)
' Application (0) Counter counter
' Application (1) DateTime placement time
' Application (2) content cache contents

Public PREFIX
Public Prefix_length

Private Sub Class_Initialize ()
PREFIX = "Cached:"
Prefix_length = 7
End Sub
Private Sub Class_Terminate
End Sub
' Set Variable
Public Property Let Cache (ByRef Key, ByRef Content)
Dim Item (2)
Item (0) = 0
Item (1) = Now ()
IF (IsObject (Content)) Then
Set Item (2) = Content
Else
Item (2) = Content
End IF
Application.UnLock
Application (PREFIX & Key) = Item
Application.Lock
End Property
' Fetch variable counter + +
Public Property Get Cache (ByRef Key)
Dim Item
Item = Application (PREFIX & Key)
IF (IsArray (Item)) Then
IF (IsObject (Item)) Then
Set Cache = Item (2)
Else
Cache = Item (2)
End IF
Application (PREFIX & Key) (0) = Application (PREFIX & Key) (0) + 1
Else
Cache = Empty
End IF
End Property
' Check if the cached object exists
Public Property Get Exists (ByRef Key)
Dim Item
Item = Application (PREFIX & Key)
IF (IsArray (Item)) Then
Exists = True
Else
Exists = False
End IF
End Property
' Get counter values
Public Property Get Counter (ByRef Key)
Dim Item
Item = Application (PREFIX & Key)
IF (IsArray (Item)) Then
Counter = Item (0)
End IF
End Property

' Set counter time
Public Property Let DateTime (ByRef Key, ByRef setdatetime)
Dim Item
Item = Application (PREFIX & Key)
IF (IsArray (Item)) Then
Item (1) = SetDateTime
End IF
End Property
' Get counter time
Public Property Get DateTime (ByRef Key)
Dim Item
Item = Application (PREFIX & Key)
IF (IsArray (Item)) Then
DateTime = Item (1)
End IF
End Property

' Reset counter
Public Sub Resetcounter ()
Dim Key
Dim Item
Application.UnLock
For each Key in application.contents
IF (Left (Key, prefix_length) = PREFIX) Then
Item = Application (Key)
Item (0) = 0
Application (Key) = Item
End IF
Next
Application.Lock
End Sub
' Delete a cache
Public Sub Clear (ByRef Key)
Application.Contents.Remove (PREFIX & Key)
End Sub
' Empty the unused cache
Public Sub clearunused ()
Dim Key, Keys, Keylength, KeyIndex
For each Key in application.contents
IF (Left (Key, prefix_length) = PREFIX) Then
IF (Application (Key) (0) = 0) Then
Keys = keys & vbNewLine & Key
End IF
End IF
Next
Keys = Split (keys, vbNewLine)
Keylength = UBound (Keys)
Application.UnLock
For KeyIndex = 1 to Keylength
Application.Contents.Remove (Keys (KeyIndex))
Next
Application.Lock
End Sub
' Empty all Caches
Public Sub ClearAll ()
Dim Key, Keys, Keylength, KeyIndex
For each Key in application.contents
IF (Left (Key, prefix_length) = PREFIX) Then
Keys = keys & vbNewLine & Key
End IF
Next
Keys = Split (keys, vbNewLine)
Keylength = UBound (Keys)
Application.UnLock
For KeyIndex = 1 to Keylength
Application.Contents.Remove (Keys (KeyIndex))
Next
Application.Lock
End Sub

End Class
Slightboyn Class Example Set wyd=new jaycache
Wyd.datetime ("Page") = time
If wyd.exists ("Page") Then
Response.Write Wyd.cache ("Page") ' Output
Else
Wyd.cache ("Page") =xxx Write
Responxe.write xxx
End IF
Wyd.clear ("page") ' Delete cache
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.