lightly 0.2.1 → 0.3.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
- SHA1:
3
- metadata.gz: d5ba4144c4735730282bdb82c67439cff20c2a43
4
- data.tar.gz: 16f60b591a382c7bf82f8d8ca34f13ff29865ae6
2
+ SHA256:
3
+ metadata.gz: 0fe5c248715cdc07ad95d167fb7fc58ed07bcdfd90d3e04ce5f0c08915e543db
4
+ data.tar.gz: 7f9d04869bbb0d5ce0936a838c6b9b93a3ae3b43f4a54e6ae69a3d9d93f53781
5
5
  SHA512:
6
- metadata.gz: 45209b3cb5ba6a15bf48f614015838e05c84bf0bba483464c429dfbbe422908455a86354c5b35f3ee3cc7e01c477736a844d513622ae1b29331ddbe7856376f6
7
- data.tar.gz: 7b87340c8b54e0f462701dd2a3bcbc30c8fd8c72b69fb2ddf478e3100fdacf19571a1108174b3fd531573f57a28dbfa78a531156abc74d0e1e5aed5d52dcb9fc
6
+ metadata.gz: e70c2a3bbdda138b737eb73026fb0f33358d869abadd798ed5aaa2cd228a5a610637df8bf5622179b63cdedb3f3dc6cea34e6722fba0c0331cc671149a9dd39a
7
+ data.tar.gz: feb6c476128a58d3787dc868c370d73e11e545fefb134163c900e9e4c16791df536ee071aa07291846d12301ff705e490e994ab4319d9a0361ba980345879baf
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  Lightly - Ruby File Cache
2
2
  ==================================================
3
3
 
4
- [![Gem](https://img.shields.io/gem/v/lightly.svg?style=flat-square)](https://rubygems.org/gems/lightly)
5
- [![Travis](https://img.shields.io/travis/DannyBen/lightly.svg?style=flat-square)](https://travis-ci.org/DannyBen/lightly)
6
- [![Code Climate](https://img.shields.io/codeclimate/github/DannyBen/lightly.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/lightly)
7
- [![Gemnasium](https://img.shields.io/gemnasium/DannyBen/lightly.svg?style=flat-square)](https://gemnasium.com/DannyBen/lightly)
4
+ [![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)
6
+ [![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
8
 
9
9
  ---
10
10
 
@@ -28,6 +28,33 @@ gem 'lightly'
28
28
  Usage
29
29
  --------------------------------------------------
30
30
 
31
+ Lightly can be used both as an instance, and as a static class.
32
+
33
+ ```ruby
34
+ require 'lightly'
35
+
36
+ # Instance
37
+ cache = Lightly.new life: '3h'
38
+ response = cache.get 'key' do
39
+ # Heavy operation here
40
+ end
41
+
42
+ # Static
43
+ Lightly.life = '3h'
44
+ Lightly.get 'key' do
45
+ # Heavy operation here
46
+ end
47
+ ```
48
+
49
+ The design intention is to provide both a globally available singleton
50
+ `Lightly` object, as well as multiple caching instances, with different
51
+ settings - depending on the use case.
52
+
53
+ Note that the examples in this README are all using the instance syntax, but
54
+ all methods are also available statically.
55
+
56
+ This is the basic usage pattern:
57
+
31
58
  ```ruby
32
59
  require 'lightly'
33
60
 
@@ -59,10 +86,20 @@ Or later:
59
86
  ```ruby
60
87
  lightly = Lightly.new
61
88
  lightly.dir = 'tmp/my_cache'
62
- lightly.life = 7200 # seconds
89
+ lightly.life = '1d'
63
90
  lightly.hash = false
64
91
  ```
65
92
 
93
+ The `life` property accepts any of these formats:
94
+
95
+ ```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
101
+ ```
102
+
66
103
  To check if a key is cached, use the `cached?` method:
67
104
 
68
105
  ```ruby
@@ -1,2 +1,4 @@
1
- require 'lightly/version'
1
+ require 'lightly/cache_operations'
2
2
  require 'lightly/lightly'
3
+
4
+ require 'byebug' if ENV['BYEBUG']
@@ -0,0 +1,104 @@
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 cached?(key)
54
+ path = get_path key
55
+ File.exist?(path) and File.size(path) > 0 and !expired?(path)
56
+ end
57
+
58
+ def enable
59
+ @enabled = true
60
+ end
61
+
62
+ def disable
63
+ @enabled = false
64
+ end
65
+
66
+ def get_path(key)
67
+ key = Digest::MD5.hexdigest(key) if hash
68
+ File.join dir, key
69
+ end
70
+
71
+ def save(key, content)
72
+ FileUtils.mkdir_p dir
73
+ path = get_path key
74
+ File.open path, 'wb' do |f|
75
+ f.write Marshal.dump content
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def load(key)
82
+ Marshal.load File.binread(get_path key)
83
+ end
84
+
85
+ def expired?(path)
86
+ expired = life > 0 && File.exist?(path) && Time.new - File.mtime(path) >= life
87
+ FileUtils.rm path if expired
88
+ expired
89
+ end
90
+
91
+ def life_to_seconds(arg)
92
+ arg = arg.to_s
93
+
94
+ case arg[-1]
95
+ when 's'; arg[0..-1].to_i
96
+ when 'm'; arg[0..-1].to_i * 60
97
+ when 'h'; arg[0..-1].to_i * 60 * 60
98
+ when 'd'; arg[0..-1].to_i * 60 * 60 * 24
99
+ else; arg.to_i
100
+ end
101
+ end
102
+
103
+ end
104
+ end
@@ -1,73 +1,7 @@
1
- require 'digest/md5'
2
- require 'fileutils'
3
-
4
1
  class Lightly
5
- attr_accessor :dir, :life, :hash
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
- def save(key, content)
55
- FileUtils.mkdir_p dir
56
- path = get_path key
57
- File.open path, 'wb' do |f|
58
- f.write Marshal.dump content
59
- end
60
- end
61
-
62
- private
63
-
64
- def load(key)
65
- Marshal.load File.binread(get_path key)
66
- end
2
+ include CacheOperations
67
3
 
68
- def expired?(path)
69
- expired = life > 0 && File.exist?(path) && Time.new - File.mtime(path) >= life
70
- FileUtils.rm path if expired
71
- expired
4
+ class << self
5
+ include CacheOperations
72
6
  end
73
7
  end
@@ -1,3 +1,3 @@
1
1
  class Lightly
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.1
4
+ version: 0.3.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: 2017-11-16 00:00:00.000000000 Z
11
+ date: 2018-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: runfile
@@ -88,6 +88,7 @@ extra_rdoc_files: []
88
88
  files:
89
89
  - README.md
90
90
  - lib/lightly.rb
91
+ - lib/lightly/cache_operations.rb
91
92
  - lib/lightly/lightly.rb
92
93
  - lib/lightly/version.rb
93
94
  homepage: https://github.com/DannyBen/lightly
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
111
  version: '0'
111
112
  requirements: []
112
113
  rubyforge_project:
113
- rubygems_version: 2.6.13
114
+ rubygems_version: 2.7.6
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: File cache for performing heavy tasks, lightly.