lightly 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5956b20bd30f21389e0f3dc3daf5c5a2362b7224
4
+ data.tar.gz: 5449ea6caf3d6388f32237911dde0efff5e58b9b
5
+ SHA512:
6
+ metadata.gz: 8fafa50075e2d06872960f7903567a3d2b90a35df632dc068df554e3e471408131a7345ed48d65ac53dfe4b649faf867fdc5f7f7a8bab7a6b403a021dbbdd7f6
7
+ data.tar.gz: edc55cef7671a634ee7fdffa6e8187f6811511a2b48b683ff20ddbd63c7e74342ad0cfe0351bd01c3eae6299767b948c854736dc3017b665d4f2293c8f77ee75
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ Lightly - Ruby File Cache
2
+ ==================================================
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)
8
+
9
+ ---
10
+
11
+ Lightly is a file cache for performing heavy tasks, lightly.
12
+
13
+ ---
14
+
15
+ Install
16
+ --------------------------------------------------
17
+
18
+ ```
19
+ $ gem install lightly
20
+ ```
21
+
22
+ Or with bundler:
23
+
24
+ ```ruby
25
+ gem 'lightly'
26
+ ```
27
+
28
+ Usage
29
+ --------------------------------------------------
30
+
31
+ ```ruby
32
+ require 'lightly'
33
+
34
+ lightly = Lightly.new
35
+
36
+ content = lightly.with 'key' do
37
+ # Heavy operation here
38
+ end
39
+ ```
40
+
41
+ This will look for a cached object with the given key and return it
42
+ if it exists and not older than 1 hour. Otherwise, it will perform the
43
+ operation inside the block, and save it to the cache object.
44
+
45
+ By default, the cached objects are stored in the `./cache` directory, and
46
+ expire after 60 minutes. The cache directory will be created as needed.
47
+
48
+ In addition, the provided key is hashed to its MD5 representation.
49
+
50
+ You can change these settings on initialization:
51
+
52
+ ```ruby
53
+ lightly = Lightly.new dir: 'tmp/my_cache', life: 7200, hash: false
54
+ ```
55
+
56
+ Or later:
57
+
58
+ ```ruby
59
+ lightly = Lightly.new
60
+ lightly.dir = 'tmp/my_cache'
61
+ lightly.life = 7200 # seconds
62
+ lightly.hash = false
63
+ ```
64
+
65
+ To check if a key is cached, use the `cached?` method:
66
+
67
+ ```ruby
68
+ lightly = Lightly.new
69
+ lightly.cached? 'example'
70
+ # => false
71
+
72
+ content = lightly.with 'example' do
73
+ open('http://example.com').read
74
+ end
75
+ lightly.cached? 'example'
76
+ # => true
77
+ ```
78
+
79
+ You can enable/disable the cache at any time:
80
+
81
+ ```ruby
82
+ lightly = Lightly.new
83
+ lightly.disable
84
+ lightly.enabled?
85
+ # => false
86
+
87
+ content = lightly.with 'example' do
88
+ open('http://example.com').read
89
+ end
90
+ lightly.cached? 'example'
91
+ # => false
92
+
93
+ lightly.enable
94
+ content = lightly.with 'example' do
95
+ open('http://example.com').read
96
+ end
97
+ lightly.cached? 'example'
98
+ # => true
99
+ ```
100
+
101
+ The `key` method is an alias to `with`, if you prefer a different wording:
102
+
103
+ ```ruby
104
+ cache = Lightly.new
105
+
106
+ content = cache.key 'example' do
107
+ # Heavy operation here
108
+ end
109
+ ```
110
+
111
+ ---
112
+
113
+ For a similar gem that provides caching specifically for HTTP downloads,
114
+ see the [WebCache gem][1]
115
+
116
+
117
+ [1]: https://github.com/DannyBen/webcache
data/lib/lightly.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'lightly/version'
2
+ require 'lightly/lightly'
@@ -0,0 +1,69 @@
1
+ require 'digest/md5'
2
+ require 'fileutils'
3
+
4
+ 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 with(key, &block)
15
+ return load key if cached?(key) && enabled?
16
+
17
+ content = block.call
18
+ save key, content if enabled?
19
+ content
20
+ end
21
+ alias_method :key, :with
22
+
23
+ def flush
24
+ return false if dir == '/' || dir.empty?
25
+ FileUtils.rm_rf dir
26
+ end
27
+
28
+ def enabled?
29
+ @enabled
30
+ end
31
+
32
+ def enable
33
+ @enabled = true
34
+ end
35
+
36
+ def disable
37
+ @enabled = false
38
+ end
39
+
40
+ def cached?(key)
41
+ path = get_path key
42
+ File.exist? path and !expired? path
43
+ end
44
+
45
+ private
46
+
47
+ def save(key, content)
48
+ FileUtils.mkdir_p dir
49
+ path = get_path key
50
+ File.open path, 'wb' do |f|
51
+ f.write Marshal.dump content
52
+ end
53
+ end
54
+
55
+ def load(key)
56
+ Marshal.load File.binread(get_path key)
57
+ end
58
+
59
+ def get_path(key)
60
+ key = Digest::MD5.hexdigest(key) if hash
61
+ File.join dir, key
62
+ end
63
+
64
+ def expired?(path)
65
+ expired = life > 0 && File.exist?(path) && Time.new - File.mtime(path) >= life
66
+ FileUtils.rm path if expired
67
+ expired
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ class Lightly
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-25 00:00:00.000000000 Z
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'
83
+ description: Easy to use file cache
84
+ email: db@dannyben.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - README.md
90
+ - lib/lightly.rb
91
+ - lib/lightly/lightly.rb
92
+ - lib/lightly/version.rb
93
+ homepage: https://github.com/DannyBen/lightly
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 2.0.0
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.6
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: File cache for performing heavy tasks, lightly.
117
+ test_files: []