lightly 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ced5fc7d04bba9051d3e8d07c3a522f47e937d90c2ada27e567fe4f3290e7dc
4
- data.tar.gz: 7d02b1c822637ca05b4e1a01c35501c3de1c1505f97a1e9f5561ac8744b146f2
3
+ metadata.gz: 1218707c4bb8d9de7bcc161073fde7a1f8af1e0f2539cec99e8600f14fa7f2b8
4
+ data.tar.gz: e06982d22591a13c917d8c91d91f70ad9606b0b4a19addea29a21c7db68b27cc
5
5
  SHA512:
6
- metadata.gz: 698a691c809a987cc45ed35ef3cbc3ee5be9d7a8a4e088b079ed04f51c0ae6c48e475bd5421534d53bf03bc905c9d76440b44b83678263307e7d084e7b00ab82
7
- data.tar.gz: 9b74b4b54267f15ddbfdaf02645f36c1d6f824726f8af827735a2aac1c5780760212dd64286c8a7284e199e93b5fa653b1e06d507a10ade3c81c4d31b3a0b9db
6
+ metadata.gz: d0e07e0f3b6a90c78b2a27564ab457b702a169cd5fcd4665d4bba39e7a2701817dc0896a5beacc911c8df2deb0cbbf26c04c29e57485efa782fd07b50fe213ce
7
+ data.tar.gz: 95d72071ede043c885fc9e86945a520d48697a4ac56af6b045739eafe711f4cb5d371cd3a557cb3263fe1a03c575a46c8f75832abc967c40afe06a386217b7a5
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
- Lightly - Ruby File Cache
2
- ==================================================
1
+ # Lightly - Ruby File Cache
3
2
 
4
3
  [![Gem Version](https://badge.fury.io/rb/lightly.svg)](https://badge.fury.io/rb/lightly)
5
- [![Build Status](https://travis-ci.com/DannyBen/lightly.svg?branch=master)](https://travis-ci.com/DannyBen/lightly)
4
+ [![Build Status](https://github.com/DannyBen/lightly/workflows/Test/badge.svg)](https://github.com/DannyBen/lightly/actions?query=workflow%3ATest)
6
5
  [![Maintainability](https://api.codeclimate.com/v1/badges/8296395c9a332a15afc7/maintainability)](https://codeclimate.com/github/DannyBen/lightly/maintainability)
7
- [![Test Coverage](https://api.codeclimate.com/v1/badges/8296395c9a332a15afc7/test_coverage)](https://codeclimate.com/github/DannyBen/lightly/test_coverage)
8
6
 
9
7
  ---
10
8
 
@@ -12,10 +10,9 @@ Lightly is a file cache for performing heavy tasks, lightly.
12
10
 
13
11
  ---
14
12
 
15
- Install
16
- --------------------------------------------------
13
+ ## Install
17
14
 
18
- ```
15
+ ```shell
19
16
  $ gem install lightly
20
17
  ```
21
18
 
@@ -25,8 +22,7 @@ Or with bundler:
25
22
  gem 'lightly'
26
23
  ```
27
24
 
28
- Usage
29
- --------------------------------------------------
25
+ ## Usage
30
26
 
31
27
  Lightly can be used both as an instance, and as a static class.
32
28
 
@@ -34,14 +30,14 @@ Lightly can be used both as an instance, and as a static class.
34
30
  require 'lightly'
35
31
 
36
32
  # Instance
37
- cache = Lightly.new life: '3h'
38
- response = cache.get 'key' do
33
+ lightly = Lightly.new life: '3h'
34
+ response = lightly.get 'key' do
39
35
  # Heavy operation here
40
36
  end
41
37
 
42
38
  # Static
43
39
  Lightly.life = '3h'
44
- Lightly.get 'key' do
40
+ response = Lightly.get 'key' do
45
41
  # Heavy operation here
46
42
  end
47
43
  ```
@@ -50,8 +46,8 @@ The design intention is to provide both a globally available singleton
50
46
  `Lightly` object, as well as multiple caching instances, with different
51
47
  settings - depending on the use case.
52
48
 
53
- Note that the examples in this README are all using the instance syntax, but
54
- all methods are also available statically.
49
+ Note that the examples in this document are all using the instance syntax,
50
+ but all methods are also available statically.
55
51
 
56
52
  This is the basic usage pattern:
57
53
 
@@ -73,12 +69,14 @@ operation inside the block, and save it to the cache object.
73
69
  By default, the cached objects are stored in the `./cache` directory, and
74
70
  expire after 60 minutes. The cache directory will be created as needed.
75
71
 
76
- In addition, the provided key is hashed to its MD5 representation.
72
+ In addition, the provided key is hashed to its MD5 representation, and the file
73
+ permissions are optionally set.
77
74
 
78
75
  You can change these settings on initialization:
79
76
 
80
77
  ```ruby
81
- lightly = Lightly.new dir: 'tmp/my_cache', life: 7200, hash: false
78
+ lightly = Lightly.new dir: 'tmp/my_cache', life: 7200,
79
+ hash: false, permissions: 0o640
82
80
  ```
83
81
 
84
82
  Or later:
@@ -88,16 +86,17 @@ lightly = Lightly.new
88
86
  lightly.dir = 'tmp/my_cache'
89
87
  lightly.life = '1d'
90
88
  lightly.hash = false
89
+ lightly.permissions = 0o640
91
90
  ```
92
91
 
93
92
  The `life` property accepts any of these formats:
94
93
 
95
94
  ```ruby
96
- cache.life = 10 # 10 seconds
97
- cache.life = '20s' # 20 seconds
98
- cache.life = '10m' # 10 minutes
99
- cache.life = '10h' # 10 hours
100
- cache.life = '10d' # 10 days
95
+ lightly.life = 10 # 10 seconds
96
+ lightly.life = '20s' # 20 seconds
97
+ lightly.life = '10m' # 10 minutes
98
+ lightly.life = '10h' # 10 hours
99
+ lightly.life = '10d' # 10 days
101
100
  ```
102
101
 
103
102
  To check if a key is cached, use the `cached?` method:
@@ -161,21 +160,28 @@ lightly = Lightly.new
161
160
  lightly.prune
162
161
  ```
163
162
 
164
- If your block returns false or nil, the data will not be cached:
163
+ If your block returns `false` or `nil`, the data will not be cached:
165
164
 
166
165
  ```ruby
167
- result = cache.get 'test' do
166
+ result = lightly.get 'test' do
168
167
  false
169
168
  end
170
169
 
171
- puts cache.cached? 'test'
170
+ puts lightly.cached? 'test'
172
171
  # => false
173
172
  ```
174
173
 
175
- ---
174
+ ## Related Projects
176
175
 
177
176
  For a similar gem that provides caching specifically for HTTP downloads,
178
- see the [WebCache gem][1]
177
+ see the [WebCache gem][webcache].
178
+
179
+ ## Contributing / Support
179
180
 
181
+ If you experience any issue, have a question or a suggestion, or if you wish
182
+ to contribute, feel free to [open an issue][issues].
183
+
184
+ ---
180
185
 
181
- [1]: https://github.com/DannyBen/webcache
186
+ [webcache]: https://github.com/DannyBen/webcache
187
+ [issues]: https://github.com/DannyBen/lightly/issues
@@ -3,19 +3,21 @@ require 'fileutils'
3
3
 
4
4
  class Lightly
5
5
  module CacheOperations
6
+ attr_accessor :permissions
6
7
  attr_writer :dir, :hash
7
8
 
8
- def initialize(dir: 'cache', life: '1h', hash: true, enabled: true)
9
+ def initialize(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil)
9
10
  @dir = dir
10
11
  @life = life_to_seconds life
11
12
  @hash = hash
12
13
  @enabled = enabled
14
+ @permissions = permissions
13
15
  end
14
16
 
15
- def get(key, &block)
17
+ def get(key)
16
18
  return load key if cached?(key) && enabled?
17
19
 
18
- content = block.call
20
+ content = yield
19
21
  save key, content if content && enabled?
20
22
  content
21
23
  end
@@ -47,17 +49,19 @@ class Lightly
47
49
 
48
50
  def flush
49
51
  return false if dir == '/' || dir.empty?
52
+
50
53
  FileUtils.rm_rf dir
51
54
  end
52
55
 
53
56
  def prune
54
57
  return false if dir == '/' || dir.empty?
58
+
55
59
  Dir["#{dir}/*"].each { |file| expired? file }
56
60
  end
57
61
 
58
62
  def cached?(key)
59
63
  path = get_path key
60
- File.exist?(path) and File.size(path) > 0 and !expired?(path)
64
+ File.exist?(path) and File.size(path).positive? and !expired?(path)
61
65
  end
62
66
 
63
67
  def enable
@@ -76,11 +80,11 @@ class Lightly
76
80
  def save(key, content)
77
81
  FileUtils.mkdir_p dir
78
82
  path = get_path key
79
- File.open path, 'wb' do |f|
80
- f.write Marshal.dump content
83
+ File.open path, 'wb', permissions do |file|
84
+ file.write Marshal.dump(content)
81
85
  end
82
86
  end
83
-
87
+
84
88
  private
85
89
 
86
90
  def load(key)
@@ -88,7 +92,7 @@ class Lightly
88
92
  end
89
93
 
90
94
  def expired?(path)
91
- expired = life > 0 && File.exist?(path) && Time.now - File.mtime(path) >= life
95
+ expired = life >= 0 && File.exist?(path) && Time.now - File.mtime(path) >= life
92
96
  FileUtils.rm path if expired
93
97
  expired
94
98
  end
@@ -97,13 +101,13 @@ class Lightly
97
101
  arg = arg.to_s
98
102
 
99
103
  case arg[-1]
100
- when 's'; arg[0..-1].to_i
101
- when 'm'; arg[0..-1].to_i * 60
102
- when 'h'; arg[0..-1].to_i * 60 * 60
103
- when 'd'; arg[0..-1].to_i * 60 * 60 * 24
104
- else; arg.to_i
104
+ when 's' then arg[0..].to_i
105
+ when 'm' then arg[0..].to_i * 60
106
+ when 'h' then arg[0..].to_i * 60 * 60
107
+ when 'd' then arg[0..].to_i * 60 * 60 * 24
108
+ else
109
+ arg.to_i
105
110
  end
106
111
  end
107
-
108
112
  end
109
113
  end
@@ -1,7 +1,7 @@
1
- class Lightly
2
- include CacheOperations
3
-
4
- class << self
5
- include CacheOperations
6
- end
7
- end
1
+ class Lightly
2
+ include CacheOperations
3
+
4
+ class << self
5
+ include CacheOperations
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  class Lightly
2
- VERSION = "0.3.2"
3
- end
2
+ VERSION = '0.4.0'
3
+ end
data/lib/lightly.rb CHANGED
@@ -1,4 +1,2 @@
1
1
  require 'lightly/cache_operations'
2
2
  require 'lightly/lightly'
3
-
4
- require 'byebug' if ENV['BYEBUG']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-23 00:00:00.000000000 Z
11
+ date: 2023-05-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easy to use file cache
14
14
  email: db@dannyben.com
@@ -24,7 +24,8 @@ files:
24
24
  homepage: https://github.com/DannyBen/lightly
25
25
  licenses:
26
26
  - MIT
27
- metadata: {}
27
+ metadata:
28
+ rubygems_mfa_required: 'true'
28
29
  post_install_message:
29
30
  rdoc_options: []
30
31
  require_paths:
@@ -33,15 +34,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
33
34
  requirements:
34
35
  - - ">="
35
36
  - !ruby/object:Gem::Version
36
- version: 2.0.0
37
+ version: '3.0'
37
38
  required_rubygems_version: !ruby/object:Gem::Requirement
38
39
  requirements:
39
40
  - - ">="
40
41
  - !ruby/object:Gem::Version
41
42
  version: '0'
42
43
  requirements: []
43
- rubyforge_project:
44
- rubygems_version: 2.7.6
44
+ rubygems_version: 3.3.26
45
45
  signing_key:
46
46
  specification_version: 4
47
47
  summary: File cache for performing heavy tasks, lightly.