gitlab-client-cache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 20d4daf1d7e01d29ee5eaaf8b1edd660ce52ec89148bfc51e8daf9908909165b
4
+ data.tar.gz: 9969132bc7cd53b70c5f4602afa9c17817c18177403bb17fbd77b743def7da3d
5
+ SHA512:
6
+ metadata.gz: 02a47cb4bc8b35df3b71e1175b3df13b98044fdaf4ebc2e76a6961261490e6954e83513e68233902c16390ee0f0a32081b1fd024495b8fc99bff494dff62bebd
7
+ data.tar.gz: 3807c92653c2c9222b8f49f914ca52f1966782f4bc8be43278d4e9c3c6ed7e4280cf0815a28527314eb2671859adfcf5b59dc6b0a2b1e7e39100ff0dcae4f759
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "cgi"
4
+ require "json"
5
+ require "pathname"
6
+
7
+ class Gitlab::Client::Cache::Caching
8
+ PATH_PATTERN = /^(..)(..)(.*)$/
9
+
10
+ def initialize(config)
11
+ @config = config
12
+ end
13
+
14
+ def cached(http_path, http_params)
15
+ path = path_to(http_path, http_params)
16
+
17
+ if path.exist? && path_fresh?(path)
18
+ load(path)
19
+ else
20
+ dump(path, yield)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def path_fresh?(path)
27
+ return true unless @config.expires_in
28
+
29
+ path.mtime + @config.expires_in >= Time.now
30
+ end
31
+
32
+ def load(path)
33
+ Marshal.load(inflate(path.read))
34
+ end
35
+
36
+ def inflate(content)
37
+ return content unless @config.compression
38
+
39
+ klass, *_params = Array(@config.compression)
40
+
41
+ Object.const_get(klass).inflate(content)
42
+ end
43
+
44
+ def dump(path, response)
45
+ path.dirname.mkpath
46
+ path.write(deflate(Marshal.dump(response)))
47
+
48
+ response
49
+ end
50
+
51
+ def deflate(content)
52
+ return content unless @config.compression
53
+
54
+ klass, *params = Array(@config.compression)
55
+
56
+ if params
57
+ Object.const_get(klass).deflate(content, *params)
58
+ else
59
+ Object.const_get(klass).deflate(content)
60
+ end
61
+ end
62
+
63
+ def path_to(http_path, http_params)
64
+ params = clean_params(http_params)
65
+ plain = JSON.generate([http_path, params])
66
+ key = Digest::SHA256.hexdigest(plain)
67
+
68
+ parts = [
69
+ @config.cache_root,
70
+ clean_path(@config.scope),
71
+ *PATH_PATTERN.match(key).captures
72
+ ].compact
73
+
74
+ Pathname.new(parts.join("/"))
75
+ end
76
+
77
+ def clean_params(params)
78
+ params = params.dup
79
+ params.delete(:logger)
80
+ params.delete(:log_level)
81
+
82
+ params
83
+ end
84
+
85
+ def clean_path(path)
86
+ CGI.escape(path)
87
+ .gsub(".", "_")
88
+ end
89
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab::Client::Cache
4
+ Config = Struct.new(:cache_root, :scope, :compression, :expires_in, keyword_init: true) do
5
+ def self.build
6
+ new(
7
+ cache_root: default_cache_root,
8
+ scope: default_scope,
9
+ compression: default_compression,
10
+ expires_in: default_expires_in
11
+ )
12
+ end
13
+
14
+ def self.default_cache_root
15
+ root = ENV.fetch("GITLAB_CLIENT_CACHE_ROOT", nil)
16
+ root ||= if ENV.key?("XDG_CACHE_HOME")
17
+ File.join(ENV.fetch("XDG_CACHE_HOME"), Process.uid.to_s)
18
+ else
19
+ File.join(File.realpath(Dir.home), ".cache")
20
+ end
21
+
22
+ File.join(root, "gitlab-cache")
23
+ end
24
+
25
+ def self.default_scope
26
+ ENV.fetch("GITLAB_CLIENT_CACHE_SCOPE", File.basename($PROGRAM_NAME))
27
+ end
28
+
29
+ def self.default_compression
30
+ "Zlib"
31
+ end
32
+
33
+ def self.default_expires_in
34
+ value = ENV.fetch("GITLAB_CLIENT_CACHE_EXPIRES_IN_SECONDS", nil)
35
+ day = 24 * 60 * 60
36
+
37
+ Integer(value || day)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gitlab::Client::Cache::Patch
4
+ def get(path, params = {})
5
+ Gitlab::Client::Cache::Caching.new(Gitlab::Client::Cache.config).cached(path, params) { super }
6
+ end
7
+ end
8
+
9
+ Gitlab::Request.singleton_class.prepend(Gitlab::Client::Cache::Patch)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabClientCache
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest/sha2"
4
+
5
+ require_relative "cache/version"
6
+ require_relative "cache/config"
7
+ require_relative "cache/caching"
8
+ require_relative "cache/patch"
9
+
10
+ module Gitlab::Client::Cache
11
+ VERSION = GitlabClientCache::VERSION
12
+
13
+ # Performs caching for `gitlab` gem.
14
+ def self.config
15
+ @config ||= Config.build
16
+ end
17
+
18
+ def self.configure
19
+ yield config if block_given?
20
+
21
+ nil
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gitlab/client/cache"
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab-client-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Leitzen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gitlab
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ description: Store fetched responses locally.
28
+ email:
29
+ - peter@leitzen.de
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/gitlab-client-cache.rb
35
+ - lib/gitlab/client/cache.rb
36
+ - lib/gitlab/client/cache/caching.rb
37
+ - lib/gitlab/client/cache/config.rb
38
+ - lib/gitlab/client/cache/patch.rb
39
+ - lib/gitlab/client/cache/version.rb
40
+ homepage: https://gitlab.com/splatteal/gitlab-client-cache
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://gitlab.com/splatteal/gitlab-client-cache
45
+ source_code_uri: https://gitlab.com/splatteal/gitlab-client-cache
46
+ changelog_uri: https://gitlab.com/splatteal/gitlab-client-cache
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 3.0.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.4.22
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Response cache for `gitlab` gem
66
+ test_files: []