Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/limited cache #41

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lua/cfc_http_restrictions/shared/url.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ function CFCHTTP.ReplaceURLs( text, f )
return html
end

local parsedAddressCache = {}
local parsedAddressCache = CFCHTTP.LimitedCache.New( 1000, 60 * 60 )
---@param url string
---@return string|nil
function CFCHTTP.GetAddress( url )
if not url then return end
local cached = parsedAddressCache[url]
local cached = parsedAddressCache:Get( url )
if cached then return cached end

local data = CFCHTTP.ParseURL( url )
parsedAddressCache[url] = data.address
parsedAddressCache:Set( url, data.address )

return data.address
end
Expand Down
79 changes: 79 additions & 0 deletions lua/includes/modules/cfchttp/limitedcache.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---@alias LimitedCacheValue {key: string, value: any, created: number}

---@class LimitedCache
---@field private data table<string, LimitedCacheValue>
---@field private queue table<number, LimitedCacheValue>
---@field private limit number
---@field private ttlSeconds number
---@field private first number
---@field private last number
local cacheIndex = {}

---@param key string
---@param value any
function cacheIndex:Set( key, value )
self:pruneQueue()

local v = {
key = key,
value = value,
created = os.time(),
}

self.data[key] = v
self:pushRight( v )
end

---@param key string
---@return any
function cacheIndex:Get( key )
local v = self.data[key]
if v then
return v.value
end
end

function cacheIndex:pushRight( value )
local last = self.last + 1
self.last = last
self.queue[last] = value
end

function cacheIndex:pruneQueue()
local amountOverLimit = self.last - self.first - self.limit

for i = self.first, self.last do
local v = self.queue[i]
if os.time() - v.created <= self.ttlSeconds and amountOverLimit <= 0 then
break
end
self.queue[i] = nil
self.data[v.key] = nil
self.first = self.first + 1
amountOverLimit = amountOverLimit - 1
end
end

---@param limit number
---@param ttlSeconds number
local function New( limit, ttlSeconds )
limit = limit or 1000
ttlSeconds = ttlSeconds or (60 * 60)
return setmetatable( {
data = {},
queue = {},

limit = limit,
ttlSeconds = ttlSeconds,

first = 0,
last = -1,
}, {
__index = cacheIndex,
} )
end

CFCHTTP = CFCHTTP or {}
CFCHTTP.LimitedCache = {
New = New,
}