Eniac0
4/11/2006 3:26:00 PM
You might be interested in the following chunks of code extracted from
my web service.
Just so you understand what im doing, let me be precise :
I have a file called "appConstants.config". That file is loaded into
cache with a dependency on the file itself.
Then when I read the file in the cache, I run a XSL statement to fetch
my constant, then I cache that value as well with a dependency on the
file too. This way I don't have to constantly run a XSL statement every
time i need that constant.
my application, the web service, calls only "GetConstant"
in the code, it looks like this :
'global variable declaration you might be interested in :
Protected Friend Shared m_LMIWS_Cache As System.Web.Caching.Cache =
System.Web.HttpRuntime.Cache()
Protected Friend Shared m_WebConfig As ConfigurationSettings
Private Shared appConstantsFile As String = "LMIWS_Constants_File"
Public Shared appConstantsFileName As String =
System.AppDomain.CurrentDomain.BaseDirectory.Replace("/", "\") &
"appConstants.config"
Private Shared xmlFile As New XmlDocument
Private Shared constantIdentifier As String = "LMIWSConst"
Private Shared commonDataKey As String = "LMIWSData"
Private Shared prestextKey As String = "LMIWSPresText"
Private Shared constantKey As String = "cacheDependencyKey"
Private Shared thisVersion As String = "1.0"
Private Shared m_strXMLRoot As String = "/LMI-IMT_CONSTANTS/VERSION"
' *****************************************************
Public Shared Function GetConstant(ByVal VersionBase As String, ByVal
XPath As String) As String
Dim tmpObj As Object
Dim strCompletePath As String = VersionBase
If Left(XPath, 1) = "/" Then
strCompletePath += XPath
Else
strCompletePath += "/" & XPath
End If
If Not (m_LMIWS_Cache(constantIdentifier & "_" & strCompletePath)
Is Nothing) Then
tmpObj = CType(m_LMIWS_Cache(constantIdentifier & "_" &
strCompletePath), Object)
Else
tmpObj = pullConstantFromFile(strCompletePath)
'Create the cache dependencies and insert the object into the
cache
If Not IsNothing(tmpObj) Then
m_LMIWS_Cache.Insert(constantIdentifier & "_" &
strCompletePath, tmpObj, New CacheDependency(appConstantsFileName))
End If
End If
Return CType(tmpObj, String)
End Function
' *****************************************************
Private Shared Function pullConstantFromFile(ByRef XPath As String)
As Object
Dim strValue As String = ""
Dim strNewXPath As String = ""
Dim strAttribName As String = ""
If m_LMIWS_Cache(appConstantsFile) Is Nothing Then
PopulateCache()
End If
Dim node As XmlNode = Nothing
Dim attribNode As XmlAttribute = Nothing
'If the last indexof is greater it means there's at least two "@".
one is version number
'the other is the one requested
If XPath.IndexOf("/@") <> -1 Then
strNewXPath = Left(XPath, XPath.LastIndexOf("@") - 1)
strAttribName = Right(XPath, XPath.Length -
XPath.LastIndexOf("@") - 1)
node = xmlFile.SelectSingleNode(strNewXPath)
If Not (node Is Nothing) Then
attribNode = node.Attributes(0)
End If
Else
node = xmlFile.SelectSingleNode(XPath)
End If
'Retrieve the value behind the node that matched the key/version
If Not (attribNode Is Nothing) Then
strValue = attribNode.InnerText()
ElseIf Not (node Is Nothing) Then
strValue = node.InnerText()
End If
Return strValue
End Function
' *****************************************************
Private Shared Sub PopulateCache()
'This will cache the entire XML File in memory as well so that we
don't access the disk
'every single time a new constant is accessed/added to the cache.
'The items added to the cache are dependant on the file itself so
if the file
'change, it will expire all current cached constants and force a
reload.
xmlFile.Load(appConstantsFileName)
m_LMIWS_Cache.Insert(appConstantsFile _
, xmlFile _
, New CacheDependency(appConstantsFileName))
End Sub