time-hash 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/time-hash.rb +53 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1b7cf6aea71447b62f158a9f54d17b1f78ff5960
|
4
|
+
data.tar.gz: c03d25e1ab06c3ee13e8953f904ce399f8d73273
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd8a6d8e00a995131a72b27c43e18f03818a9f27ab329ffc5658bbadc9fbdbe7fd3e5f18abd64d16361a94df10e6d8310cf136f2c684fb1dc3dd7e0c0a7e8c36
|
7
|
+
data.tar.gz: 878eef6f772526db96c4d627abbb7ddf362d03b7825f253c8cb9c7519431aeef30b69fb0270335e2a8bec4db27c4b2aba16d94e26c377808c81d6fd60a0da21d
|
data/lib/time-hash.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'time'
|
2
|
+
|
3
|
+
class TimeHash
|
4
|
+
|
5
|
+
attr_accessor :automatic_expiration
|
6
|
+
attr_reader :times
|
7
|
+
|
8
|
+
def initialize(automatic_expiration = true)
|
9
|
+
@automatic_expiration = automatic_expiration
|
10
|
+
@times = {}
|
11
|
+
@internal = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def expire!
|
15
|
+
@times.clone.each do |key, times|
|
16
|
+
if expired?(key)
|
17
|
+
@internal.delete(key)
|
18
|
+
@times.delete(key)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def expired?(key)
|
25
|
+
return true unless @times.has_key?(key)
|
26
|
+
time, ttl = @times[key]
|
27
|
+
Time.now - time > ttl
|
28
|
+
end
|
29
|
+
|
30
|
+
def put(key, value, ttl) # ms
|
31
|
+
expire!
|
32
|
+
response = (@internal[key] = value)
|
33
|
+
@times[key] = [Time.now, ttl]
|
34
|
+
response
|
35
|
+
end
|
36
|
+
|
37
|
+
ConfusionError = Class.new(StandardError)
|
38
|
+
|
39
|
+
def []=(*args)
|
40
|
+
raise(ConfusionError,
|
41
|
+
"Don't use standard key-value assignment with TimeHashes, please.")
|
42
|
+
end
|
43
|
+
|
44
|
+
# before sending to any methods on the wrapped hash,
|
45
|
+
# expire things.
|
46
|
+
Hash.instance_methods.each do |method|
|
47
|
+
next if method == :[]=
|
48
|
+
define_method(method) do |*args, &block|
|
49
|
+
expire! if @automatic_expiration
|
50
|
+
@internal.send(method, *args, &block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: time-hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Larisch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Set a timeout on key-value pairs, at which point delete them.
|
14
|
+
email: jameslarisch@protonmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/time-hash.rb
|
20
|
+
homepage: http://rubygems.org/gems/time-hash
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Hash with time-expiration.
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|