lightly 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -2
- data/lib/lightly/cache_operations.rb +17 -13
- data/lib/lightly/lightly.rb +7 -7
- data/lib/lightly/version.rb +2 -2
- data/lib/lightly.rb +0 -2
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1218707c4bb8d9de7bcc161073fde7a1f8af1e0f2539cec99e8600f14fa7f2b8
|
4
|
+
data.tar.gz: e06982d22591a13c917d8c91d91f70ad9606b0b4a19addea29a21c7db68b27cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0e07e0f3b6a90c78b2a27564ab457b702a169cd5fcd4665d4bba39e7a2701817dc0896a5beacc911c8df2deb0cbbf26c04c29e57485efa782fd07b50fe213ce
|
7
|
+
data.tar.gz: 95d72071ede043c885fc9e86945a520d48697a4ac56af6b045739eafe711f4cb5d371cd3a557cb3263fe1a03c575a46c8f75832abc967c40afe06a386217b7a5
|
data/README.md
CHANGED
@@ -69,12 +69,14 @@ operation inside the block, and save it to the cache object.
|
|
69
69
|
By default, the cached objects are stored in the `./cache` directory, and
|
70
70
|
expire after 60 minutes. The cache directory will be created as needed.
|
71
71
|
|
72
|
-
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.
|
73
74
|
|
74
75
|
You can change these settings on initialization:
|
75
76
|
|
76
77
|
```ruby
|
77
|
-
lightly = Lightly.new dir: 'tmp/my_cache', life: 7200,
|
78
|
+
lightly = Lightly.new dir: 'tmp/my_cache', life: 7200,
|
79
|
+
hash: false, permissions: 0o640
|
78
80
|
```
|
79
81
|
|
80
82
|
Or later:
|
@@ -84,6 +86,7 @@ lightly = Lightly.new
|
|
84
86
|
lightly.dir = 'tmp/my_cache'
|
85
87
|
lightly.life = '1d'
|
86
88
|
lightly.hash = false
|
89
|
+
lightly.permissions = 0o640
|
87
90
|
```
|
88
91
|
|
89
92
|
The `life` property accepts any of these formats:
|
@@ -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
|
17
|
+
def get(key)
|
16
18
|
return load key if cached?(key) && enabled?
|
17
19
|
|
18
|
-
content =
|
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)
|
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 |
|
80
|
-
|
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)
|
@@ -97,13 +101,13 @@ class Lightly
|
|
97
101
|
arg = arg.to_s
|
98
102
|
|
99
103
|
case arg[-1]
|
100
|
-
when 's'
|
101
|
-
when 'm'
|
102
|
-
when 'h'
|
103
|
-
when 'd'
|
104
|
-
else
|
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
|
data/lib/lightly/lightly.rb
CHANGED
@@ -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
|
data/lib/lightly/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
class Lightly
|
2
|
-
VERSION =
|
3
|
-
end
|
2
|
+
VERSION = '0.4.0'
|
3
|
+
end
|
data/lib/lightly.rb
CHANGED
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.
|
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:
|
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,14 +34,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
34
|
requirements:
|
34
35
|
- - ">="
|
35
36
|
- !ruby/object:Gem::Version
|
36
|
-
version:
|
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
|
-
rubygems_version: 3.
|
44
|
+
rubygems_version: 3.3.26
|
44
45
|
signing_key:
|
45
46
|
specification_version: 4
|
46
47
|
summary: File cache for performing heavy tasks, lightly.
|