easycache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +59 -0
  3. data/lib/easycache.rb +71 -0
  4. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ea977cf6a6a27a16734292e29d1e2c6a999a349584be9329992122286476f57
4
+ data.tar.gz: 4849b2cf1b564da41304e6e3e393d215c5267771445e5320311332f7ff86e180
5
+ SHA512:
6
+ metadata.gz: 3053b6f55221322790fe99230b7bd939f482dc95f4fd16550a700f73a1939b19e5682080be6417aaa398d687576e7cd2f27cdc54d299df844b88b14166d4313f
7
+ data.tar.gz: d1d26932c40eaaa986bca6ef3066242c2289932e1831cf2a1d9380b7e6ba9b0bc52a85eff8664e06700cd5874757562393fab57b023242974721ad289ce6c9a1
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # EasyCache
2
+ <p align="center">
3
+
4
+ [![Gem Version](https://badge.fury.io/rb/easy-cache.svg)](https://badge.fury.io/rb/easy-cache)
5
+ ![License](https://img.shields.io/badge/license-AGPL%203.0-blue.svg)
6
+ [![Lint](https://github.com/malvads/easy-cache/actions/workflows/lint.yml/badge.svg)](https://github.com/malvads/easy-cache/actions/workflows/lint.yml)
7
+ [![Tests](https://github.com/malvads/easy-cache/actions/workflows/tests.yml/badge.svg)](https://github.com/malvads/easy-cache/actions/workflows/tests.yml)
8
+ [![Build](https://github.com/malvads/easy-cache/actions/workflows/build.yml/badge.svg)](https://github.com/malvads/easy-cache/actions/workflows/build.yml)
9
+ </p>
10
+ EasyCache is an in-memory cache system for Ruby designed for situations where you don't want to set up Redis or Memcached but still need a simple solution for caching key-value data.
11
+
12
+ ## Install
13
+
14
+ ```
15
+ gem install easy-cache
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ To use EasyCache in your Ruby project, require the library and include it in your code:
21
+
22
+ ```ruby
23
+ require 'easycache'
24
+
25
+ cache = EasyCache.new
26
+ ```
27
+
28
+ ## Storing data
29
+
30
+ ```ruby
31
+ key = "my_key"
32
+ cache_ttl = 3600
33
+ store_in_mem = true
34
+ data = cache.fetch(cache_key, cache_ttl, store_in_mem) do
35
+ my_http_get
36
+ end
37
+ ```
38
+
39
+ Now data is in-mem for the next 3600 second (store_in_mem variable is important for storing data first time).
40
+
41
+ ## Getting data
42
+
43
+ If i want to get the data stored in mem i do
44
+
45
+ ```ruby
46
+ data = cache.fetch("my_key")
47
+ ```
48
+
49
+ or i can also re-call the same function
50
+
51
+ ```ruby
52
+ data = cache.fetch("my_key", cache_ttl, store_in_mem) do
53
+ my_http_get
54
+ end
55
+ ```
56
+
57
+ because the data is already cached, so it will not call the block, it will return the cached data instead.
58
+
59
+ This will output the cached data, remember that cached data is stored in mem for only 3600 seconds
data/lib/easycache.rb ADDED
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Affero General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Affero General Public License for more details.
12
+
13
+ # You should have received a copy of the GNU Affero General Public License
14
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
+
16
+ #
17
+ # This is the core of EasyCache.
18
+ # EasyCache provides an easy-to-use, in-memory cache system for Ruby.
19
+ # It is designed for situations where you don't want to set up Redis or Memcached but still want
20
+ # a simple solution for caching key-value data.
21
+ #
22
+ class EasyCache
23
+ def initialize
24
+ @cache = {}
25
+ @mutex = Mutex.new
26
+ end
27
+
28
+ def fetch(key, expiration = 3600, store_in_cache = false) # rubocop:disable Style/OptionalBooleanParameter
29
+ @mutex.synchronize do
30
+ return cached_value(key, store_in_cache) if cache_contains_valid_data?(key) && !block_given?
31
+
32
+ if should_fetch_from_block?(key, store_in_cache)
33
+ value = block_given? ? yield : nil
34
+ cache_value(key, value, expiration) if value && store_in_cache
35
+ value
36
+ else
37
+ cached_value(key, store_in_cache)
38
+ end
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def cache_contains_valid_data?(key)
45
+ @cache.key?(key) && !expired?(key)
46
+ end
47
+
48
+ def should_fetch_from_block?(key, store_in_cache)
49
+ !store_in_cache || (!@cache.key?(key) || expired?(key))
50
+ end
51
+
52
+ def cached_value(key, _store_in_cache)
53
+ @cache[key][:value]
54
+ end
55
+
56
+ def cache_value(key, value, expiration)
57
+ @cache[key] = { value: value, timestamp: Time.now, expiration: expiration }
58
+ end
59
+
60
+ def expired?(key)
61
+ return false unless @cache.key?(key) && @cache[key].key?(:timestamp) && @cache[key].key?(:expiration)
62
+
63
+ expiration_date = @cache[key][:timestamp] + @cache[key][:expiration]
64
+ if Time.now > expiration_date
65
+ @cache.delete(key)
66
+ true
67
+ else
68
+ false
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easycache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Miguel Álvarez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple gem for store and manage data in mem
14
+ email: thegexi@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - README.md
20
+ - lib/easycache.rb
21
+ homepage: https://github.com/malvads/easy-cache
22
+ licenses:
23
+ - AGPL-3.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.7.0
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.2.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Easy to use in-mem cache system for ruby
44
+ test_files: []