Skip to content

Commit 30e582f

Browse files
authored
Merge pull request #3487 from Vizzuality/feature/rails-cache-keys
feature/rails-cache-keys
2 parents 1df942d + 53a50e3 commit 30e582f

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

app/controllers/application_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base
33
protect_from_forgery with: :exception
44

55
before_action :check_browser, if: proc { Rails.env.production? }
6+
before_action :cache_keys
67

78
def not_found
89
raise ActionController::RoutingError.new('Not Found')
@@ -14,6 +15,10 @@ def accept_terms
1415
@title = 'Terms of Service'
1516
end
1617

18+
def cache_keys
19+
@cache_keys = $redis.keys('*')
20+
end
21+
1722
private
1823

1924
def check_browser

app/javascript/utils/request.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ import { getKey, addKey } from 'services/cache';
55
import CACHE_EXCEPTIONS from 'data/cache-exceptions.json';
66

77
const EXPIRE_DEFAULT = 86400;
8+
let cacheKeys = [];
89

910
const request = {
1011
get(url, expire = EXPIRE_DEFAULT, exceptionId = null) {
12+
if (cacheKeys.length) {
13+
cacheKeys = window.RequestCache.keys;
14+
}
1115
const key = btoa(url);
12-
return axios.get(`/cache/${key}`).then(cache => {
13-
const { data } = cache;
14-
if (!data) {
15-
const axiosInstance = axios.create();
16-
const haveException = checkException(exceptionId);
17-
if (!haveException) {
18-
axiosInstance.interceptors.response.use(response =>
19-
addKey(key, response.data, expire, exceptionId)
20-
.then(() => response)
21-
.catch(() => response)
22-
);
23-
}
24-
return axiosInstance.get(url);
16+
if (cacheKeys.indexOf(key) === -1) {
17+
const axiosInstance = axios.create();
18+
const haveException = checkException(exceptionId);
19+
if (!haveException) {
20+
axiosInstance.interceptors.response.use(response =>
21+
addKey(key, response.data, expire, exceptionId)
22+
.then(() => response)
23+
.catch(() => response)
24+
);
2525
}
26-
return getKey(key);
27-
});
26+
return axiosInstance.get(url);
27+
}
28+
return getKey(key);
2829
}
2930
};
3031

app/views/layouts/application_react.html.erb

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<link rel="canonical" href="<%= request.original_url %>" />
1313
<script src="https://cdn.optimizely.com/js/7988050790.js"></script>
1414
<%= render 'shared/gfw_assets' %>
15+
<%= render 'shared/cache_keys' %>
1516
<%= render 'shared/track' %>
1617

1718
<%= yield :head %>

app/views/shared/_cache_keys.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script type="text/javascript">
2+
window.RequestCache = {
3+
keys: <%= raw @cache_keys %>
4+
};
5+
</script>

0 commit comments

Comments
 (0)