Caching

Forum for things that doesn't really have anything to do with hMailServer. Such as php.ini, beer, etc etc.
Post Reply
User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Caching

Post by RvdH » 2024-06-18 15:32

I am experimenting with something... I created another Component that utilizes the .NET MemoryCache

For now I am running some test but to give you an idea how this can be used within hMailserver scripting is like:

Code: Select all

Sub OnClientConnect(oClient)
    With CreateObject("MemoryCaching.Component")
        On Error Resume Next
        dim value
        value = .GetCache(oClient.IPAddress, "GEO")
        If Not (IsEmpty(value)) Then
            Exit Sub
        End IF
        If Err.Number <> 0 Then
            EventLog.Write("MemoryCaching Error: " & Err.Description)
            Err.Clear
        End If
        On Error Goto 0
    End With
    
    ... rest of your functions
End Sub

Code: Select all

Sub OnHELO(oClient)
    With CreateObject("MemoryCaching.Component")
        On Error Resume Next
        dim value
        value = .GetCache(oClient.IPAddress, "GEO")
        If Not (IsEmpty(value)) Then
            Exit Sub
        End IF
        If Err.Number <> 0 Then
            EventLog.Write("MemoryCaching Error: " & Err.Description)
            Err.Clear
        End If
        On Error Goto 0
    End With
    
    ... rest of your functions
End Sub
And finally:

Code: Select all

Sub OnClientLogon(oClient)
    If Not oClient.Authenticated Then
        ... rest of your functions
    Else
        Dim strGeo : strGeo = GeoIPLookup(oClient.IPAddress)
        strRegEx = "^(BE|NL)$" 
        If Lookup(strRegEx, strGeo) Then
            With CreateObject("MemoryCaching.Component")
                On Error Resume Next
                dim value
                value = .GetCache(oClient.IPAddress, "GEO")
                If (IsEmpty(value)) Then
                    call .SetCache(oClient.IPAddress, strGeo, 3600, "GEO")
                End if
                If Err.Number <> 0 Then
                    EventLog.Write("MemoryCaching Error: " & Err.Description)
                    Err.Clear
                End If
                On Error Goto 0
            End With
        End If

        ... rest of your functions
    End If
End Sub
Which stores both the client IP as the CountryCode in the MemoryCache.

Turning the values stored in the MemoryCache into a sort of client whitelist/trustedlist, eg: autheticated IP's from trusted countries, eg: NL and BE are stored in MemoryCache for 1 hour (3600) seconds
The component uses SlidingExpiration, meaning a span of time within which a cache entry must be accessed before the cache entry is evicted from the cache.
Effectivly meaning that as long a trusted client connects (OnClientConnect) to check POP/IMAP mailboxes or sends a mail (OnClientConnect, OnHELO) the value stored in the cache is being reset to it's initial value and wont expire, whilst by checking the MemoryCache trusted clients can be excluded (Exit Sub) from additional scripts/checks you might have in OnClientConnect and OnHELO
Last edited by RvdH on 2024-06-18 15:49, edited 1 time in total.
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-18 15:47

Currently running a debug version which is very verbose and writes to Trace which can be read with DebugView, but this how the MemoryCache looks like

Code: Select all

[8680] [MemoryCachingComponent]: GetCache, key: [GEO]XXX.93.189.94, value: NL 
[8680] [MemoryCachingComponent]: GetCount, identifier: GEO, items: 46 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.228, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.86.235.9, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.141.121, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.81, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.163.96.101, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.32.242.100, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.205.136.198, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.255.51.191, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.247, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.171.15.146, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.110.76, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.132.255.136, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.211, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.169.157.180, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.227, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.250.166.113, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.167, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.98.224.53, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.85.82.132, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.255.50.142, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.145.253, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.87.102.202, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.93.81.224, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.240.152.172, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.161.192.139, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.169.71.146, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.107, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.111.31, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.255, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.93.189.94, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.114.8, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.241.199.218, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.4, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.161.192.204, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.247.45.188, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.234, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.36.159.223, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.81.253, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.68.185.24, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.188, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.196.76, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.167, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.142, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.131.46.221, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.85.213, value: NL 
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
SorenR
Senior user
Senior user
Posts: 6367
Joined: 2006-08-21 15:38
Location: Denmark

Re: Caching

Post by SorenR » 2024-06-18 15:53

RvdH wrote:
2024-06-18 15:47
Currently running a debug version which is very verbose and writes to Trace which can be read with DebugView, but this how the MemoryCache looks like

Code: Select all

[8680] [MemoryCachingComponent]: GetCache, key: [GEO]XXX.93.189.94, value: NL 
[8680] [MemoryCachingComponent]: GetCount, identifier: GEO, items: 46 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.228, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.86.235.9, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.141.121, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.81, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.163.96.101, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.32.242.100, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.205.136.198, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.255.51.191, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.247, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.171.15.146, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.110.76, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.132.255.136, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.211, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.169.157.180, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.227, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.250.166.113, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.167, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.98.224.53, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.85.82.132, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.255.50.142, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.145.253, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.87.102.202, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.93.81.224, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.240.152.172, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.161.192.139, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.169.71.146, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.107, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.111.31, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.255, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.93.189.94, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.114.8, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.241.199.218, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.4, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.161.192.204, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.247.45.188, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.234, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.36.159.223, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.81.253, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.68.185.24, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.188, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.196.76, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.167, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.142, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.131.46.221, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.85.213, value: NL 
So, can it be used globally with this?

https://hmailserver.com/forum/viewtopic ... 74#p209774
SørenR.

Woke is Marxism advancing through Maoist cultural revolution.

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-18 16:00

Whilst normally this won't work, eg: when running a regular vbs from within cscript.exe file as the cache is destroyed when the cscript.exe ends, for hmailserver this is not the case as it uses the PID from the parent application, which in this case is hMailServer.exe meaning the cache is retained throughout the time hmailserver is running. The cache is destroyed when you restart hmailserver.exe however.
Last edited by RvdH on 2024-06-18 16:12, edited 1 time in total.
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
SorenR
Senior user
Senior user
Posts: 6367
Joined: 2006-08-21 15:38
Location: Denmark

Re: Caching

Post by SorenR » 2024-06-18 16:05

RvdH wrote:
2024-06-18 16:00
Whilst normally this won't work, eg: when running a regular vbs from within cscript.exe file as the cache is destroyed when the cscript.exe ends, for hmailserver this is not the case as it uses the PID from the parent application, which in this case is hMailServer.exe meaning the cache is retained throughout the time hmailserver is running. The cache is detroyed when you restart hmailserver.exe however.
That would work... So in theory you could also do session throtteling in realtime ?
SørenR.

Woke is Marxism advancing through Maoist cultural revolution.

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-18 16:08

SorenR wrote:
2024-06-18 15:53
RvdH wrote:
2024-06-18 15:47
Currently running a debug version which is very verbose and writes to Trace which can be read with DebugView, but this how the MemoryCache looks like

Code: Select all

[8680] [MemoryCachingComponent]: GetCache, key: [GEO]XXX.93.189.94, value: NL 
[8680] [MemoryCachingComponent]: GetCount, identifier: GEO, items: 46 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.228, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.86.235.9, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.141.121, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.81, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.163.96.101, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.32.242.100, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.205.136.198, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.255.51.191, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.247, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.171.15.146, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.110.76, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.132.255.136, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.211, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.169.157.180, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.227, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.250.166.113, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.167, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.98.224.53, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.85.82.132, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.255.50.142, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.145.253, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.87.102.202, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.93.81.224, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.240.152.172, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.161.192.139, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.169.71.146, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.84.107, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.111.31, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.255, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.93.189.94, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.207.114.8, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.241.199.218, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.4, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.161.192.204, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.247.45.188, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.234, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.36.159.223, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.81.253, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.68.185.24, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.188, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.99.196.76, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.82.167, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.83.142, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.131.46.221, value: NL 
[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [GEO]XXX.63.85.213, value: NL 
So, can it be used globally with this?

https://hmailserver.com/forum/viewtopic ... 74#p209774
Should be possible, i tried to make it as versatile as i possibly could, storing a string, optionally with indentifier (GEO in above example) and a object, eg: string, int, bool

Code: Select all

dim block : block = True  [boolean]
If iShouldBlock Then
    call .SetCache(oClient.IPAddress, block, 3600, "DBW")
end if

Code: Select all

[8680] [MemoryCachingComponent]: CacheKeyValuePair, key: [DBW]XXX.63.85.213, value: True
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-18 16:18

Only downside i found this far, allthough the value is stored as a object within the cache the component always returns it as string, so you have to do the type casting manualy, Cint(value), CBool(value)
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-18 16:34

SorenR wrote:
2024-06-18 16:05
RvdH wrote:
2024-06-18 16:00
Whilst normally this won't work, eg: when running a regular vbs from within cscript.exe file as the cache is destroyed when the cscript.exe ends, for hmailserver this is not the case as it uses the PID from the parent application, which in this case is hMailServer.exe meaning the cache is retained throughout the time hmailserver is running. The cache is detroyed when you restart hmailserver.exe however.
That would work... So in theory you could also do session throtteling in realtime ?
Session throtteling? Only one entry per key is stored in cache, key can be IP Address or any other string (with optional indentifier) and a value

Here another example, which is where i initially wrote the component for, it is a very old Classic ASP website (hosted by company i work for) that contantly is being hammered by (bad) bots...
I do a AbuseIPDB check on the visitors IP Address and then store the "Confidence of Abuse Level" into the MemoryCache (This also works in IIS as the PID is taken from the parent w3wp.exe)
For every visitor/cache entry with a "Confidence of Abuse Level" score >= 20 i return a 401 error, the MemoryCache in this scenario prevents excessive lookups to abusipdb.com API

Code: Select all

[8328] [MemoryCachingComponent]: GetCache, key: [BOT]XXX.249.78.10, value: 0 
[8328] [MemoryCachingComponent]: GetCount, identifier: BOT, items: 34 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.67.227.235, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.20.83.133, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.77.199, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.72.97, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.171.251.115, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.31.115.187, value: 45 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.75.19, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.69.64, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.77.13, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.113.128.147, value: 37 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.77.167.4, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.167.144.198, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.46.13.102, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.145.165.227, value: 39 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.44.147.62, value: 100 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.76.43, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.245.188.175, value: 38 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.75.21, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.67.239.132, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.145.227.243, value: 43 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.76.2, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.160.71.11, value: 32 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.63.3.58, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.81.87.90, value: 75 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.94.247.30, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.128.79.59, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.77.169, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.99.231, value: 34 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.138.105.133, value: 65 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.78.10, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.77.168, value: 11 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.77.160, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.215.128.209, value: 0 
[8328] [MemoryCachingComponent]: CacheKeyValuePair, key: [BOT]XXX.249.76.44, value: 13 
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-20 11:22

Here is a first "test" version, needs .NET Framework 4.8

Code: Select all

Create object of type MemoryCaching.Component
This object has 8 public functions:

	-- Version() * returns the version number
	-- Help() * shows this help
	-- Boolean AddCache(<key>, <value>, <seconds> [optional, <identifier>]) * add the specified CacheItem value
	-- Method AddOrGetCache(<key>, <value>, <seconds> [optional, <identifier>]) * add or get the specified CacheItem value
	-- Method ClearCache([optional, <identifier>]) * clears the Cache
	-- Object GetCache(<key> [optional, <identifier>]) * get the specified CacheItem value
	-- Boolean RemoveCache(<key> [optional, <identifier>]) * remove the specified CacheItem value
	-- Method SetCache(<key>, <value>, <seconds> [optional, <identifier>]) * set the specified CacheItem value
	-- String ShowCache([optional, <identifier>]) * shows the Memory Cache contents
https://d-fault.nl/files/MemoryCachingComponent.zip

Notes:
  1. The AddCache function call does nothing (returns false) if there is already a value for that key. SetCache does an insert or update, as necessary. RemoveCache + AddCache would leave a gap in the middle when another thread querying that key would get no clue (SetCache does not; the swap is typically atomic); as such, while SetCache has the same end result as RemoveCache + AddCache, the mechanism difference is important since it could impact other callers.
  2. The AddOrGetCache method overloads are used to insert a cache entry into the cache. If a cache entry with a matching key does not exist, these methods insert a new entry. If a cache entry with a matching key already exists, they return the existing entry.
Curious what usage ideas (within hMailserver) you guys can come up with :wink: 8)


FYI, Windows Defender detects, Trojan:Win32/Phonzy.C!ml... apparently this is a 'false positive' triggered on the InnoSetup Installer, not sure what i can do to prevent that
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-20 12:18

The 'identifier' is used to distinguish between different keys, the 'identifier' value is prefixed (between square brackets, eg: []) to the key.
This can be useful if you have various types of keys/values that are being stored in the cache simultaneously, using the identifier you can Clear(), ShowCache() etc. for just the key/value types that match this 'identifier'
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

palinka
Senior user
Senior user
Posts: 4619
Joined: 2017-09-12 17:57

Re: Caching

Post by palinka » 2024-06-20 17:15

RvdH wrote:
2024-06-20 11:22
Curious what usage ideas (within hMailserver) you guys can come up with :wink: 8)
I haven't really read the thread close enough to really understand what this does, but do you think it could be possible to assign a unique ID at client connection, then follow that through all the various events?

User avatar
RvdH
Senior user
Senior user
Posts: 3344
Joined: 2008-06-27 14:42
Location: The Netherlands

Re: Caching

Post by RvdH » 2024-06-20 17:55

palinka wrote:
2024-06-20 17:15
RvdH wrote:
2024-06-20 11:22
Curious what usage ideas (within hMailserver) you guys can come up with :wink: 8)
I haven't really read the thread close enough to really understand what this does, but do you think it could be possible to assign a unique ID at client connection, then follow that through all the various events?
Not sure why you would want that... but you always could use oClient.SessionID (5.7 + 5.6 mod) in the key
The memory CacheItem 'key' already is unique, eg: there can only exist one key (possibly with identifier), see the examples above, the unique IP address is the 'key'

the more data is stored in cache, the bigger the chance you will notice that in memory usage, and therefor i want to keep it as small as possible
CIDR to RegEx: d-fault.nl/cidrtoregex
DNS Lookup: d-fault.nl/dnstools
DKIM Generator: d-fault.nl/dkimgenerator
DNSBL Lookup: d-fault.nl/dnsbllookup
GEOIP Lookup: d-fault.nl/geoiplookup

palinka
Senior user
Senior user
Posts: 4619
Joined: 2017-09-12 17:57

Re: Caching

Post by palinka » 2024-06-20 18:09

RvdH wrote:
2024-06-20 17:55
palinka wrote:
2024-06-20 17:15
RvdH wrote:
2024-06-20 11:22
Curious what usage ideas (within hMailserver) you guys can come up with :wink: 8)
I haven't really read the thread close enough to really understand what this does, but do you think it could be possible to assign a unique ID at client connection, then follow that through all the various events?
Not sure why you would want that... but you always could use oClient.SessionID (5.7 + 5.6 mod) in the key
The memory CacheItem 'key' already is unique, eg: there can only exist one key (possibly with identifier), see the examples above, the unique IP address is the 'key'

the more data is stored in cache, the bigger the chance you will notice that in memory usage, and therefor i want to keep it as small as possible
Thanks. I didn't know that existed.

Post Reply