|
| 1 | +(ns cache.cache |
| 2 | + (:require [cache.filestorage :as storage] |
| 3 | + [file.system :as file] |
| 4 | + [cache.log :as log] |
| 5 | + [clojure.string :as string])) |
| 6 | + |
| 7 | +(defn cachetype->tag [type] |
| 8 | + (or |
| 9 | + (get {"collections" "COLLECTION_DATA" |
| 10 | + "config_webservices" "WEBSERVICE" |
| 11 | + "layout" "LAYOUT_GENERAL_CACHE_TAG" |
| 12 | + "full_page" "FPC" |
| 13 | + "config_integration_consolidated" "INTEGRATION_CONSOLIDATED" |
| 14 | + "config_integration_api" "INTEGRATION_API_CONFIG" |
| 15 | + "config_integration" "INTEGRATION"} type) |
| 16 | + (string/upper-case type))) |
| 17 | + |
| 18 | +(defn- match-name? [name-pattern file] |
| 19 | + (cond |
| 20 | + (regexp? name-pattern) (re-find name-pattern file) |
| 21 | + :else (= name-pattern (file/basename file)))) |
| 22 | + |
| 23 | +(defn- file-fingerprint-fn [name-pattern content-head-pattern] |
| 24 | + (fn [file] |
| 25 | + (and (match-name? name-pattern file) |
| 26 | + (file/exists? file) |
| 27 | + (or (nil? content-head-pattern) |
| 28 | + (re-find content-head-pattern (file/head file)))))) |
| 29 | + |
| 30 | +(defn- tuples->fingerprint-fns [type tuples] |
| 31 | + (reduce (fn [acc [filename content]] |
| 32 | + (assoc acc (file-fingerprint-fn filename content) type)) {} tuples)) |
| 33 | + |
| 34 | +(defn- config-filetypes [] |
| 35 | + (let [t [["di.xml" #"urn:magento:framework:ObjectManager/etc/config\.xsd"] |
| 36 | + ["crontab.xml" #"urn:magento:module:Magento_Cron:etc/crontab\.xsd"] |
| 37 | + ["events.xml" #"urn:magento:framework:Event/etc/events\.xsd"] |
| 38 | + ["extension_attributes.xml" #"urn:magento:framework:Api/etc/extension_attributes\.xsd"] |
| 39 | + ["routes.xml" #"urn:magento:framework:App/etc/routes\.xsd"] |
| 40 | + ["widget.xml" #"urn:magento:module:Magento_Widget:etc/widget\.xsd"] |
| 41 | + ["product_types.xml" #"urn:magento:module:Magento_Catalog:etc/product_types\.xsd"] |
| 42 | + ["product_options.xml" #"urn:magento:module:Magento_Catalog:etc/product_options\.xsd"] |
| 43 | + ["payment.xml" #"urn:magento:module:Magento_Payment:etc/payment\.xsd"] |
| 44 | + ["search_request.xml" #"urn:magento:framework:Search/etc/search_request\.xsd"] |
| 45 | + ["config.xml" #"urn:magento:module:Magento_Store:etc/config\.xsd"] |
| 46 | + [#"/ui_component/.+\.xml$" #"urn:magento:module:Magento_Ui:etc/ui_configuration\.xsd"] |
| 47 | + ["menu.xml" #"urn:magento:module:Magento_Backend:etc/menu\.xsd"] |
| 48 | + ["acl.xml" #"urn:magento:framework:Acl/etc/acl\.xsd"] |
| 49 | + ["indexer.xml" #"urn:magento:framework:Indexer/etc/indexer\.xsd"]]] |
| 50 | + (tuples->fingerprint-fns ::config t))) |
| 51 | + |
| 52 | +(defn- layout-filetypes [] |
| 53 | + (let [t [[#"/layout/.+\.xml$" #"<page [^>]+\"urn:magento:framework:View/Layout/etc/page_configuration\.xsd\""]]] |
| 54 | + (tuples->fingerprint-fns ::layout t))) |
| 55 | + |
| 56 | +(defn- translation-filetypes [] |
| 57 | + (let [t [[#"/i18n/.+\.csv$" #".+,.+"]]] |
| 58 | + (tuples->fingerprint-fns ::translation t))) |
| 59 | + |
| 60 | +(defn- template-filetypes [] |
| 61 | + (let [t [[#"/templates/.+\.phtml"]]] |
| 62 | + (tuples->fingerprint-fns ::template t))) |
| 63 | + |
| 64 | +(def file->type |
| 65 | + (merge (config-filetypes) |
| 66 | + (layout-filetypes) |
| 67 | + (translation-filetypes) |
| 68 | + (template-filetypes))) |
| 69 | + |
| 70 | +(defn- magefile->filetype [file] |
| 71 | + (reduce (fn [_ [filetype? type]] |
| 72 | + (when (filetype? file) (reduced type))) nil file->type)) |
| 73 | + |
| 74 | +(defn tag->ids [tag] |
| 75 | + (if (file/exists? (storage/tag->filepath tag)) |
| 76 | + (storage/tag->ids tag) |
| 77 | + [])) |
| 78 | + |
| 79 | +(defn id->file [id] |
| 80 | + (storage/id->filepath id)) |
| 81 | + |
| 82 | +(def filetype->cachetypes |
| 83 | + {::config ["config"] |
| 84 | + ::translation ["translate"] |
| 85 | + ::layout ["layout" "full_page"] |
| 86 | + ::template ["block_html" "full_page"]}) |
| 87 | + |
| 88 | +(defn magefile->cachetypes [file] |
| 89 | + (let [filetype (magefile->filetype file)] |
| 90 | + (get filetype->cachetypes filetype []))) |
| 91 | + |
| 92 | +(defn- rm-tagfile [tag] |
| 93 | + (let [file (storage/tag->filepath tag)] |
| 94 | + (when (file/exists? file) |
| 95 | + (file/rm file)))) |
| 96 | + |
| 97 | +(defn- clean |
| 98 | + ([] (storage/clean-all)) |
| 99 | + ([type] |
| 100 | + (let [tag (cachetype->tag type)] |
| 101 | + (log/debug "Cleaning tag" tag) |
| 102 | + (run! storage/delete (tag->ids tag)) |
| 103 | + (rm-tagfile tag))) |
| 104 | + ([type & types] |
| 105 | + (run! #(clean %) (into [type] types)))) |
| 106 | + |
| 107 | +(defn clean-cache-types [cache-types] |
| 108 | + (apply log/notice "Cleaning cache type(s)" cache-types) |
| 109 | + (when (or (empty? cache-types) (not= ["full_page"] cache-types)) |
| 110 | + (log/debug "Using cache dir var/cache...") |
| 111 | + (binding [storage/*cachedir* "var/cache/"] |
| 112 | + (let [cache-types (remove #(= "full_page" %) cache-types)] |
| 113 | + (apply clean cache-types)))) |
| 114 | + (when (or (empty? cache-types) (some #{"full_page"} cache-types)) |
| 115 | + (log/debug "Using cache dir var/page_cache...") |
| 116 | + (binding [storage/*cachedir* "var/page_cache/"] |
| 117 | + (clean)))) |
0 commit comments