philiprehberger-cache_kit 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 969b99417559919665d91aea64820dd9373d787020b4d966b2443d0099864da0
4
+ data.tar.gz: '09e639031c7c4b3864e45dfe35ece4e29d109d062c4b0ebca424f06269f5b14b'
5
+ SHA512:
6
+ metadata.gz: f15036e4a4f6cd77fd5e9f6af3c8f668eb4dee397415bbdeeda94d8acac24e7e7f81cfc6cc06721b4621ea4f1f043b0f468d1c44bef745ec14b8c01600b545f9
7
+ data.tar.gz: 771c1d2c89759a190c242c1cf6b12fca3673050d8ddca0e0cdee746d7daa73be7fe81b945dc945f69a7eae43aef3e991d32c4cb0c51d4304f79faa27af6ad0ba
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this gem will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-03-10
11
+
12
+ ### Added
13
+ - Initial release
14
+ - Thread-safe in-memory LRU cache
15
+ - TTL expiration per entry
16
+ - Tag-based bulk invalidation
17
+ - `get`, `set`, `fetch`, `delete`, `clear` operations
18
+ - Configurable max size with LRU eviction
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 philiprehberger
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # philiprehberger-cache_kit
2
+
3
+ [![Tests](https://github.com/philiprehberger/rb-cache-kit/actions/workflows/ci.yml/badge.svg)](https://github.com/philiprehberger/rb-cache-kit/actions/workflows/ci.yml)
4
+ [![Gem Version](https://badge.fury.io/rb/philiprehberger-cache_kit.svg)](https://rubygems.org/gems/philiprehberger-cache_kit)
5
+
6
+ In-memory LRU cache with TTL, tags, and thread safety for Ruby.
7
+
8
+ ## Requirements
9
+
10
+ - Ruby >= 3.1
11
+
12
+ ## Installation
13
+
14
+ Add to your Gemfile:
15
+
16
+ ```ruby
17
+ gem "philiprehberger-cache_kit"
18
+ ```
19
+
20
+ Then run:
21
+
22
+ ```bash
23
+ bundle install
24
+ ```
25
+
26
+ Or install directly:
27
+
28
+ ```bash
29
+ gem install philiprehberger-cache_kit
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ```ruby
35
+ require "philiprehberger/cache_kit"
36
+
37
+ cache = Philiprehberger::CacheKit::Store.new(max_size: 500)
38
+
39
+ # Basic get/set
40
+ cache.set("user:1", { name: "Alice" }, ttl: 300)
41
+ cache.get("user:1") # => { name: "Alice" }
42
+ ```
43
+
44
+ ### Fetch (get or compute)
45
+
46
+ ```ruby
47
+ user = cache.fetch("user:1", ttl: 300) do
48
+ User.find(1)
49
+ end
50
+ ```
51
+
52
+ ### TTL Expiration
53
+
54
+ ```ruby
55
+ cache.set("session", "abc", ttl: 60) # expires in 60 seconds
56
+ cache.get("session") # => "abc"
57
+ # ... 60 seconds later ...
58
+ cache.get("session") # => nil
59
+ ```
60
+
61
+ ### Tag-based Invalidation
62
+
63
+ ```ruby
64
+ cache.set("user:1", data1, tags: ["users"])
65
+ cache.set("user:2", data2, tags: ["users"])
66
+ cache.set("post:1", data3, tags: ["posts"])
67
+
68
+ cache.invalidate_tag("users") # removes user:1 and user:2, keeps post:1
69
+ ```
70
+
71
+ ### LRU Eviction
72
+
73
+ ```ruby
74
+ cache = Philiprehberger::CacheKit::Store.new(max_size: 3)
75
+
76
+ cache.set("a", 1)
77
+ cache.set("b", 2)
78
+ cache.set("c", 3)
79
+ cache.set("d", 4) # evicts "a" (least recently used)
80
+
81
+ cache.get("a") # => nil
82
+ cache.get("d") # => 4
83
+ ```
84
+
85
+ ## API
86
+
87
+ | Method | Description |
88
+ |--------|-------------|
89
+ | `Store.new(max_size: 1000)` | Create a cache with max entries |
90
+ | `Store#get(key)` | Get a value (nil if missing/expired) |
91
+ | `Store#set(key, value, ttl:, tags:)` | Store a value |
92
+ | `Store#fetch(key, ttl:, tags:, &block)` | Get or compute a value |
93
+ | `Store#delete(key)` | Delete a key |
94
+ | `Store#invalidate_tag(tag)` | Remove all entries with a tag |
95
+ | `Store#clear` | Remove all entries |
96
+ | `Store#size` | Number of entries |
97
+ | `Store#key?(key)` | Check if a key exists and is not expired |
98
+
99
+ ## Development
100
+
101
+ ```bash
102
+ bundle install
103
+ bundle exec rspec # Run tests
104
+ bundle exec rubocop # Check code style
105
+ ```
106
+
107
+ ## License
108
+
109
+ MIT
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module CacheKit
5
+ # Internal cache entry with value, TTL, and tags.
6
+ class Entry
7
+ attr_reader :value, :tags, :created_at
8
+
9
+ # @param value the cached value
10
+ # @param ttl [Numeric, nil] time-to-live in seconds (nil = no expiry)
11
+ # @param tags [Array<String>] tags for bulk invalidation
12
+ def initialize(value, ttl: nil, tags: [])
13
+ @value = value
14
+ @ttl = ttl
15
+ @tags = tags.map(&:to_s)
16
+ @created_at = Time.now
17
+ end
18
+
19
+ # Check if the entry has expired.
20
+ #
21
+ # @return [Boolean]
22
+ def expired?
23
+ return false if @ttl.nil?
24
+
25
+ (Time.now - @created_at) >= @ttl
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module CacheKit
5
+ # Thread-safe in-memory LRU cache with TTL and tag-based invalidation.
6
+ class Store
7
+ # @param max_size [Integer] maximum number of entries (LRU eviction when exceeded)
8
+ def initialize(max_size: 1000)
9
+ @max_size = max_size
10
+ @data = {}
11
+ @order = []
12
+ @mutex = Mutex.new
13
+ end
14
+
15
+ # Get a value by key. Returns nil if missing or expired.
16
+ #
17
+ # @param key [String] the cache key
18
+ # @return the cached value, or nil
19
+ def get(key)
20
+ @mutex.synchronize do
21
+ entry = @data[key]
22
+ return nil unless entry
23
+
24
+ if entry.expired?
25
+ remove_entry(key)
26
+ return nil
27
+ end
28
+
29
+ touch(key)
30
+ entry.value
31
+ end
32
+ end
33
+
34
+ # Store a value.
35
+ #
36
+ # @param key [String] the cache key
37
+ # @param value the value to cache
38
+ # @param ttl [Numeric, nil] time-to-live in seconds
39
+ # @param tags [Array<String>] tags for bulk invalidation
40
+ # @return the stored value
41
+ def set(key, value, ttl: nil, tags: [])
42
+ @mutex.synchronize do
43
+ remove_entry(key) if @data.key?(key)
44
+ evict if @data.size >= @max_size
45
+
46
+ @data[key] = Entry.new(value, ttl: ttl, tags: tags)
47
+ @order.push(key)
48
+ value
49
+ end
50
+ end
51
+
52
+ # Get or compute a value.
53
+ #
54
+ # @param key [String] the cache key
55
+ # @param ttl [Numeric, nil] TTL for newly computed values
56
+ # @param tags [Array<String>] tags for newly computed values
57
+ # @yield computes the value if not cached
58
+ # @return the cached or computed value
59
+ def fetch(key, ttl: nil, tags: [], &block)
60
+ value = get(key)
61
+ return value unless value.nil?
62
+
63
+ computed = block.call
64
+ set(key, computed, ttl: ttl, tags: tags)
65
+ computed
66
+ end
67
+
68
+ # Delete a specific key.
69
+ #
70
+ # @param key [String] the cache key
71
+ # @return [Boolean] true if the key existed
72
+ def delete(key)
73
+ @mutex.synchronize do
74
+ removed = @data.key?(key)
75
+ remove_entry(key)
76
+ removed
77
+ end
78
+ end
79
+
80
+ # Invalidate all entries with a given tag.
81
+ #
82
+ # @param tag [String] the tag to invalidate
83
+ # @return [Integer] number of entries removed
84
+ def invalidate_tag(tag)
85
+ @mutex.synchronize do
86
+ tag_s = tag.to_s
87
+ keys = @data.select { |_, entry| entry.tags.include?(tag_s) }.keys
88
+ keys.each { |k| remove_entry(k) }
89
+ keys.size
90
+ end
91
+ end
92
+
93
+ # Clear all entries.
94
+ #
95
+ # @return [void]
96
+ def clear
97
+ @mutex.synchronize do
98
+ @data.clear
99
+ @order.clear
100
+ end
101
+ end
102
+
103
+ # @return [Integer] number of entries (including expired ones not yet evicted)
104
+ def size
105
+ @mutex.synchronize { @data.size }
106
+ end
107
+
108
+ # @param key [String]
109
+ # @return [Boolean] true if the key exists and is not expired
110
+ def key?(key)
111
+ @mutex.synchronize do
112
+ entry = @data[key]
113
+ return false unless entry
114
+ return false if entry.expired?
115
+
116
+ true
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ def touch(key)
123
+ @order.delete(key)
124
+ @order.push(key)
125
+ end
126
+
127
+ def evict
128
+ oldest = @order.first
129
+ remove_entry(oldest) if oldest
130
+ end
131
+
132
+ def remove_entry(key)
133
+ @data.delete(key)
134
+ @order.delete(key)
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Philiprehberger
4
+ module CacheKit
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cache_kit/version"
4
+ require_relative "cache_kit/entry"
5
+ require_relative "cache_kit/store"
6
+
7
+ module Philiprehberger
8
+ module CacheKit
9
+ class Error < StandardError; end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: philiprehberger-cache_kit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Philip Rehberger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-03-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A lightweight, thread-safe in-memory LRU cache with TTL expiration and
14
+ tag-based bulk invalidation for Ruby applications.
15
+ email:
16
+ - me@philiprehberger.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - LICENSE
23
+ - README.md
24
+ - lib/philiprehberger/cache_kit.rb
25
+ - lib/philiprehberger/cache_kit/entry.rb
26
+ - lib/philiprehberger/cache_kit/store.rb
27
+ - lib/philiprehberger/cache_kit/version.rb
28
+ homepage: https://github.com/philiprehberger/rb-cache-kit
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/philiprehberger/rb-cache-kit
33
+ source_code_uri: https://github.com/philiprehberger/rb-cache-kit
34
+ changelog_uri: https://github.com/philiprehberger/rb-cache-kit/blob/main/CHANGELOG.md
35
+ rubygems_mfa_required: 'true'
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 3.1.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.5.22
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: In-memory LRU cache with TTL, tags, and thread safety
55
+ test_files: []