Skip to content

Commit b4e0c8b

Browse files
selfxpddragosd
authored andcommitted
The same cache store should't be added multiple times to the cache stores list (#8)
* request_caching only reads from memory * added remoteCacheStatus class for retrieving a healthy Redis backend * updated the test with docker to figure out a possible IP for the Redis docker container * updated Makefile to only clean the Redis docker instances when running tests * Updated `make install` with the new folder `status` * The same cache store should't be added multiple times to the cache stores list * Updated log message after suggestions from review
1 parent b12f81b commit b4e0c8b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/lua/api-gateway/cache/cache.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ function _M:new(o)
3131
return o
3232
end
3333

34+
---
35+
-- See if the the cache already exists inside the cache store
36+
-- @param store
37+
--
38+
function _M:hasStore(store)
39+
if (store == nil) then
40+
ngx.log(ngx.DEBUG, "Store to be compared is nil")
41+
return false
42+
end
43+
for _, cache_store in ipairs(self.__cache_stores) do
44+
if (cache_store == nil) then
45+
ngx.log(ngx.DEBUG, "Store from cache store is nil" )
46+
return false
47+
end
48+
ngx.log(ngx.DEBUG, "Comparing store named: ", store:getName(), " with existing store named: ", cache_store:getName())
49+
if (cache_store:getName() == store:getName()) then
50+
return true
51+
end
52+
end
53+
return false
54+
end
55+
3456
--- Adds a cache into the cache. The order in which the caches are added
3557
-- is important when reading / writing from/to cache.
3658
-- @param store An instance of api-gateway.cache.store object.
@@ -41,6 +63,11 @@ function _M:addStore(store)
4163
return nil
4264
end
4365

66+
if (self:hasStore(store)) then
67+
ngx.log(ngx.WARN, "Attempt to add a cache store that already exists: ", store:getName())
68+
return nil
69+
end
70+
4471
local count = #self.__cache_stores
4572
self.__cache_stores[count + 1] = store
4673
return self.__cache_stores

test/perl/api-gateway/cache/cache.t

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,19 @@ __DATA__
6666
assert(stores[1]:getName() == "local_cache", "1st store should be names local_store but it is " .. tostring(stores[1]:getName()))
6767
assert(stores[2]:getName() == "redis_hash_cache", "2nd store should be names redis_hash_cache but it is " .. tostring(stores[2]:getName()))
6868
69+
assert(cache:hasStore(local_cache), "local_cache store should already be defined in the cache stores")
70+
71+
cache:addStore(local_cache)
72+
assert(table.getn(stores) == 2, "getStores() should return 2 stores")
73+
6974
ngx.say("OK")
7075
';
7176
}
7277
7378
--- timeout: 5s
7479
--- request
7580
GET /t
76-
--- response_body_like eval
81+
--- response_body_like
7782
["OK"]
7883
--- error_code: 200
7984
--- no_error_log

0 commit comments

Comments
 (0)