lightly 0.2.0 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +65 -18
- data/lib/lightly.rb +3 -1
- data/lib/lightly/cache_operations.rb +109 -0
- data/lib/lightly/lightly.rb +3 -69
- data/lib/lightly/version.rb +1 -1
- metadata +5 -75
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d313f9708367bd12cd91e8a40edfead09bdab9abb923f1ba1b7cbd5129ff61e5
|
4
|
+
data.tar.gz: 6b19b9bf03a20d1725c5a9ef018a1e75fb945860195b158052dc74a0a0cc3de9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b1836e246ed0b7d2e3212fd9a273f390df4f34c992b1817ba326e11f12a23303c02c40bd398a3d633eb37c0761a59b7b268c125ef6d3b472a436ff93345b52d
|
7
|
+
data.tar.gz: 7023d499f4fa07b16c4580a0f6507970898cd507c097c1c7a88638174afe101c356e0435b290f78f0aedea8d578ccde2cea06fb3390ecf7b530275eaf4b74cf8
|
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
Lightly - Ruby File Cache
|
2
|
-
==================================================
|
1
|
+
# Lightly - Ruby File Cache
|
3
2
|
|
4
|
-
[![Gem](https://
|
5
|
-
[![
|
6
|
-
[![
|
7
|
-
[![Gemnasium](https://img.shields.io/gemnasium/DannyBen/lightly.svg?style=flat-square)](https://gemnasium.com/DannyBen/lightly)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/lightly.svg)](https://badge.fury.io/rb/lightly)
|
4
|
+
[![Build Status](https://github.com/DannyBen/lightly/workflows/Test/badge.svg)](https://github.com/DannyBen/lightly/actions?query=workflow%3ATest)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/8296395c9a332a15afc7/maintainability)](https://codeclimate.com/github/DannyBen/lightly/maintainability)
|
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,34 @@ Or with bundler:
|
|
25
22
|
gem 'lightly'
|
26
23
|
```
|
27
24
|
|
28
|
-
Usage
|
29
|
-
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Lightly can be used both as an instance, and as a static class.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'lightly'
|
31
|
+
|
32
|
+
# Instance
|
33
|
+
lightly = Lightly.new life: '3h'
|
34
|
+
response = lightly.get 'key' do
|
35
|
+
# Heavy operation here
|
36
|
+
end
|
37
|
+
|
38
|
+
# Static
|
39
|
+
Lightly.life = '3h'
|
40
|
+
response = Lightly.get 'key' do
|
41
|
+
# Heavy operation here
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
The design intention is to provide both a globally available singleton
|
46
|
+
`Lightly` object, as well as multiple caching instances, with different
|
47
|
+
settings - depending on the use case.
|
48
|
+
|
49
|
+
Note that the examples in this document are all using the instance syntax,
|
50
|
+
but all methods are also available statically.
|
51
|
+
|
52
|
+
This is the basic usage pattern:
|
30
53
|
|
31
54
|
```ruby
|
32
55
|
require 'lightly'
|
@@ -59,10 +82,20 @@ Or later:
|
|
59
82
|
```ruby
|
60
83
|
lightly = Lightly.new
|
61
84
|
lightly.dir = 'tmp/my_cache'
|
62
|
-
lightly.life =
|
85
|
+
lightly.life = '1d'
|
63
86
|
lightly.hash = false
|
64
87
|
```
|
65
88
|
|
89
|
+
The `life` property accepts any of these formats:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
lightly.life = 10 # 10 seconds
|
93
|
+
lightly.life = '20s' # 20 seconds
|
94
|
+
lightly.life = '10m' # 10 minutes
|
95
|
+
lightly.life = '10h' # 10 hours
|
96
|
+
lightly.life = '10d' # 10 days
|
97
|
+
```
|
98
|
+
|
66
99
|
To check if a key is cached, use the `cached?` method:
|
67
100
|
|
68
101
|
```ruby
|
@@ -117,21 +150,35 @@ lightly = Lightly.new
|
|
117
150
|
lightly.clear 'example'
|
118
151
|
```
|
119
152
|
|
120
|
-
|
153
|
+
To clear all expired keys, call:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
lightly = Lightly.new
|
157
|
+
lightly.prune
|
158
|
+
```
|
159
|
+
|
160
|
+
If your block returns `false` or `nil`, the data will not be cached:
|
121
161
|
|
122
162
|
```ruby
|
123
|
-
result =
|
163
|
+
result = lightly.get 'test' do
|
124
164
|
false
|
125
165
|
end
|
126
166
|
|
127
|
-
puts
|
167
|
+
puts lightly.cached? 'test'
|
128
168
|
# => false
|
129
169
|
```
|
130
170
|
|
131
|
-
|
171
|
+
## Related Projects
|
132
172
|
|
133
173
|
For a similar gem that provides caching specifically for HTTP downloads,
|
134
|
-
see the [WebCache gem][
|
174
|
+
see the [WebCache gem][webcache].
|
135
175
|
|
176
|
+
## Contributing / Support
|
177
|
+
|
178
|
+
If you experience any issue, have a question or a suggestion, or if you wish
|
179
|
+
to contribute, feel free to [open an issue][issues].
|
180
|
+
|
181
|
+
---
|
136
182
|
|
137
|
-
[
|
183
|
+
[webcache]: https://github.com/DannyBen/webcache
|
184
|
+
[issues]: https://github.com/DannyBen/lightly/issues
|
data/lib/lightly.rb
CHANGED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
class Lightly
|
5
|
+
module CacheOperations
|
6
|
+
attr_writer :dir, :hash
|
7
|
+
|
8
|
+
def initialize(dir: 'cache', life: '1h', hash: true, enabled: true)
|
9
|
+
@dir = dir
|
10
|
+
@life = life_to_seconds life
|
11
|
+
@hash = hash
|
12
|
+
@enabled = enabled
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(key, &block)
|
16
|
+
return load key if cached?(key) && enabled?
|
17
|
+
|
18
|
+
content = block.call
|
19
|
+
save key, content if content && enabled?
|
20
|
+
content
|
21
|
+
end
|
22
|
+
|
23
|
+
def life
|
24
|
+
@life ||= 3600
|
25
|
+
end
|
26
|
+
|
27
|
+
def life=(new_life)
|
28
|
+
@life = life_to_seconds new_life
|
29
|
+
end
|
30
|
+
|
31
|
+
def dir
|
32
|
+
@dir ||= 'cache'
|
33
|
+
end
|
34
|
+
|
35
|
+
def hash?
|
36
|
+
@hash ||= (@hash.nil? ? true : @hash)
|
37
|
+
end
|
38
|
+
|
39
|
+
def enabled?
|
40
|
+
@enabled ||= (@enabled.nil? ? true : @enabled)
|
41
|
+
end
|
42
|
+
|
43
|
+
def clear(key)
|
44
|
+
path = get_path key
|
45
|
+
FileUtils.rm path if File.exist? path
|
46
|
+
end
|
47
|
+
|
48
|
+
def flush
|
49
|
+
return false if dir == '/' || dir.empty?
|
50
|
+
FileUtils.rm_rf dir
|
51
|
+
end
|
52
|
+
|
53
|
+
def prune
|
54
|
+
return false if dir == '/' || dir.empty?
|
55
|
+
Dir["#{dir}/*"].each { |file| expired? file }
|
56
|
+
end
|
57
|
+
|
58
|
+
def cached?(key)
|
59
|
+
path = get_path key
|
60
|
+
File.exist?(path) and File.size(path) > 0 and !expired?(path)
|
61
|
+
end
|
62
|
+
|
63
|
+
def enable
|
64
|
+
@enabled = true
|
65
|
+
end
|
66
|
+
|
67
|
+
def disable
|
68
|
+
@enabled = false
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_path(key)
|
72
|
+
key = Digest::MD5.hexdigest(key) if hash?
|
73
|
+
File.join dir, key
|
74
|
+
end
|
75
|
+
|
76
|
+
def save(key, content)
|
77
|
+
FileUtils.mkdir_p dir
|
78
|
+
path = get_path key
|
79
|
+
File.open path, 'wb' do |f|
|
80
|
+
f.write Marshal.dump content
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def load(key)
|
87
|
+
Marshal.load File.binread(get_path key)
|
88
|
+
end
|
89
|
+
|
90
|
+
def expired?(path)
|
91
|
+
expired = life >= 0 && File.exist?(path) && Time.now - File.mtime(path) >= life
|
92
|
+
FileUtils.rm path if expired
|
93
|
+
expired
|
94
|
+
end
|
95
|
+
|
96
|
+
def life_to_seconds(arg)
|
97
|
+
arg = arg.to_s
|
98
|
+
|
99
|
+
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
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
data/lib/lightly/lightly.rb
CHANGED
@@ -1,73 +1,7 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
require 'fileutils'
|
3
|
-
|
4
1
|
class Lightly
|
5
|
-
|
6
|
-
|
7
|
-
def initialize(opts={})
|
8
|
-
@dir = opts[:dir] || 'cache'
|
9
|
-
@life = opts[:life] || 3600
|
10
|
-
@hash = opts.key?(:hash) ? opts[:hash] : true
|
11
|
-
@enabled = true
|
12
|
-
end
|
13
|
-
|
14
|
-
def get(key, &block)
|
15
|
-
return load key if cached?(key) && enabled?
|
16
|
-
|
17
|
-
content = block.call
|
18
|
-
save key, content if content && enabled?
|
19
|
-
content
|
20
|
-
end
|
21
|
-
|
22
|
-
def clear(key)
|
23
|
-
path = get_path key
|
24
|
-
FileUtils.rm path if File.exist? path
|
25
|
-
end
|
26
|
-
|
27
|
-
def flush
|
28
|
-
return false if dir == '/' || dir.empty?
|
29
|
-
FileUtils.rm_rf dir
|
30
|
-
end
|
31
|
-
|
32
|
-
def enabled?
|
33
|
-
@enabled
|
34
|
-
end
|
35
|
-
|
36
|
-
def cached?(key)
|
37
|
-
path = get_path key
|
38
|
-
File.exist?(path) and File.size(path) > 0 and !expired?(path)
|
39
|
-
end
|
40
|
-
|
41
|
-
def enable
|
42
|
-
@enabled = true
|
43
|
-
end
|
44
|
-
|
45
|
-
def disable
|
46
|
-
@enabled = false
|
47
|
-
end
|
48
|
-
|
49
|
-
def get_path(key)
|
50
|
-
key = Digest::MD5.hexdigest(key) if hash
|
51
|
-
File.join dir, key
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
def save(key, content)
|
57
|
-
FileUtils.mkdir_p dir
|
58
|
-
path = get_path key
|
59
|
-
File.open path, 'wb' do |f|
|
60
|
-
f.write Marshal.dump content
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def load(key)
|
65
|
-
Marshal.load File.binread(get_path key)
|
66
|
-
end
|
2
|
+
include CacheOperations
|
67
3
|
|
68
|
-
|
69
|
-
|
70
|
-
FileUtils.rm path if expired
|
71
|
-
expired
|
4
|
+
class << self
|
5
|
+
include CacheOperations
|
72
6
|
end
|
73
7
|
end
|
data/lib/lightly/version.rb
CHANGED
metadata
CHANGED
@@ -1,85 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lightly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.3
|
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:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: runfile
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0.7'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0.7'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: runfile-tasks
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0.4'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0.4'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '3.4'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.4'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: simplecov
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0.11'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0.11'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: byebug
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '9.0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '9.0'
|
11
|
+
date: 2020-06-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
83
13
|
description: Easy to use file cache
|
84
14
|
email: db@dannyben.com
|
85
15
|
executables: []
|
@@ -88,6 +18,7 @@ extra_rdoc_files: []
|
|
88
18
|
files:
|
89
19
|
- README.md
|
90
20
|
- lib/lightly.rb
|
21
|
+
- lib/lightly/cache_operations.rb
|
91
22
|
- lib/lightly/lightly.rb
|
92
23
|
- lib/lightly/version.rb
|
93
24
|
homepage: https://github.com/DannyBen/lightly
|
@@ -109,8 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
40
|
- !ruby/object:Gem::Version
|
110
41
|
version: '0'
|
111
42
|
requirements: []
|
112
|
-
|
113
|
-
rubygems_version: 2.6.6
|
43
|
+
rubygems_version: 3.1.2
|
114
44
|
signing_key:
|
115
45
|
specification_version: 4
|
116
46
|
summary: File cache for performing heavy tasks, lightly.
|