Skip to content

Commit

Permalink
The same cache store should't be added multiple times to the cache st…
Browse files Browse the repository at this point in the history
…ores 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
  • Loading branch information
selfxp authored and ddragosd committed Apr 21, 2016
1 parent b12f81b commit b4e0c8b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/lua/api-gateway/cache/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ function _M:new(o)
return o
end

---
-- See if the the cache already exists inside the cache store
-- @param store
--
function _M:hasStore(store)
if (store == nil) then
ngx.log(ngx.DEBUG, "Store to be compared is nil")
return false
end
for _, cache_store in ipairs(self.__cache_stores) do
if (cache_store == nil) then
ngx.log(ngx.DEBUG, "Store from cache store is nil" )
return false
end
ngx.log(ngx.DEBUG, "Comparing store named: ", store:getName(), " with existing store named: ", cache_store:getName())
if (cache_store:getName() == store:getName()) then
return true
end
end
return false
end

--- Adds a cache into the cache. The order in which the caches are added
-- is important when reading / writing from/to cache.
-- @param store An instance of api-gateway.cache.store object.
Expand All @@ -41,6 +63,11 @@ function _M:addStore(store)
return nil
end

if (self:hasStore(store)) then
ngx.log(ngx.WARN, "Attempt to add a cache store that already exists: ", store:getName())
return nil
end

local count = #self.__cache_stores
self.__cache_stores[count + 1] = store
return self.__cache_stores
Expand Down
7 changes: 6 additions & 1 deletion test/perl/api-gateway/cache/cache.t
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ __DATA__
assert(stores[1]:getName() == "local_cache", "1st store should be names local_store but it is " .. tostring(stores[1]:getName()))
assert(stores[2]:getName() == "redis_hash_cache", "2nd store should be names redis_hash_cache but it is " .. tostring(stores[2]:getName()))
assert(cache:hasStore(local_cache), "local_cache store should already be defined in the cache stores")
cache:addStore(local_cache)
assert(table.getn(stores) == 2, "getStores() should return 2 stores")
ngx.say("OK")
';
}
--- timeout: 5s
--- request
GET /t
--- response_body_like eval
--- response_body_like
["OK"]
--- error_code: 200
--- no_error_log
Expand Down

0 comments on commit b4e0c8b

Please sign in to comment.