Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- minime (11:58) 260/0 $ cat etc/trafficserver/yum_cache.lua
- -- Licensed to the Apache Software Foundation (ASF) under one
- -- or more contributor license agreements. See the NOTICE file
- -- distributed with this work for additional information
- -- regarding copyright ownership. The ASF licenses this file
- -- to you under the Apache License, Version 2.0 (the
- -- "License"); you may not use this file except in compliance
- -- with the License. You may obtain a copy of the License at
- --
- -- http://www.apache.org/licenses/LICENSE-2.0
- --
- -- Unless required by applicable law or agreed to in writing, software
- -- distributed under the License is distributed on an "AS IS" BASIS,
- -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- -- See the License for the specific language governing permissions and
- -- limitations under the License.
- -- Some helper functions, this might be useful to move over to a Lua library, that
- -- we can load from several of these ATS scripts
- local function ends_with(str, ending)
- return ending == "" or str:sub(-(#ending)) == ending
- end
- local function ends_with_which(str, suffixes)
- for _, suf in ipairs(suffixes) do
- if ends_with(str, suf) then
- return suf
- end
- end
- return nil
- end
- -- This is the list of RPM suffixes that we should cache aggressively
- local YUM_SUFFIXES = {".rpm", ".srpm", ".drpm"}
- local YUM_DEBUG_TAG = "yum_cache"
- -- The Fedora repo is like (always?) to return a 302 redirect to one of the available
- -- mirror. In order for this to work for us, we have to follow such redirects. 3 is an
- -- arbitrarily picked number, 1 ought to work in most (all?) cases. It's important that
- -- we cache this object with the original URL as the cache key though!
- function do_remap()
- local uri_path = ts.client_request.get_uri()
- local yum_suffix = ends_with_which(uri_path, YUM_SUFFIXES)
- ts.debug(YUM_DEBUG_TAG, "Turning on follow-redirect for this request")
- ts.http.config_int_set(TS_LUA_CONFIG_HTTP_NUMBER_OF_REDIRECTIONS, 3)
- ts.http.config_int_set(TS_LUA_CONFIG_HTTP_REDIRECT_USE_ORIG_CACHE_KEY, 1)
- -- This is useful to turn off the HTTP cache when testing things
- -- ts.http.config_int_set(TS_LUA_CONFIG_HTTP_CACHE_HTTP, 0)
- if yum_suffix ~= nil then
- ts.debug(YUM_DEBUG_TAG, "Adding read-response-header hook, YUM suffix is " .. yum_suffix)
- ts.hook(TS_LUA_HOOK_READ_RESPONSE_HDR, on_read_response)
- end
- return 0
- end
- -- On origin responses, we might want to tweak the Cache-Control: max-age= header, if
- -- one is not already present.
- function on_read_response()
- local response = ts.server_response.get_status()
- if response == 200 then
- local cc_header = ts.server_response.header["Cache-Control"]
- if cc_header == nil then
- -- Add a cache max-age of 30 days
- ts.server_response.header["Cache-Control"] = "max-age=2592000"
- end
- end
- return 0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement