Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
#29: Check cache staleness whenever a tree is requested.
Browse files Browse the repository at this point in the history
  • Loading branch information
danports committed Apr 20, 2020
1 parent f1eda66 commit f5a693f
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions apis/github
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,26 @@ Release.tree = function(self)
end

-- A class for a repository
local __repoPriv = setmetatable({}, {mode='k'})
local Repository = {}
Repository.__index = Repository
Repository.new = function(user, name, auth)
local r = setmetatable({user=user, name=name, auth=auth}, Repository)
__repoPriv[r] = {trees={}}
return r
return setmetatable({
user = user,
name = name,
auth = auth,
_treeCache = {}
}, Repository)
end
Repository.tree = function(self, sha)
sha = sha or "master"
if not __repoPriv[self].trees[sha] then
__repoPriv[self].trees[sha] = Tree.new(self, sha)
local cachedTree = _treeCache[sha]
local newTree = Tree.new(self, sha)
newTree:getContents() -- Force resolution of commit-ish
if cachedTree and newTree.sha == cachedTree.sha then
return cachedTree
end
return __repoPriv[self].trees[sha]
_treeCache[sha] = newTree
return newTree
end
local function releaseFromURL(url, repo)
local status, data = getAPI(url, repo.auth)
Expand Down

0 comments on commit f5a693f

Please sign in to comment.