memcachepod 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 63e4f41dfa41db87514ab48a0177512aa40ef64a
4
+ data.tar.gz: 1e21e5f83b81a4dfaf1a4d47f86b4e0c40c5a116
5
+ SHA512:
6
+ metadata.gz: dbc36eaaad05a2409a72d49000f42be594174d5f1ffb8626468e56d9824ed16e8ae53de01a2b62a125af736d3aec0eecdb105a3af0e596b7ebf37ecbb473d1ca
7
+ data.tar.gz: f28f1e1bdf3dc95816eb76834c006310bfd0b7b869d90b21b7f841a959191b2cc14b16e86b4da7ce9415551d7fef35aac47ff35475acc31ce63959cd47e6d28b
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+ gem "rspec"
3
+ gem "rake"
4
+ gem "simplecov"
5
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Kiyoka Nishiyama
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # memcachepod
2
+ In-process caching library that provide memcached compatible interface.
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ # encoding: ascii
4
+ module MemcachePod
5
+ class Client
6
+
7
+ ##
8
+ # MemcachePod::Client is the main class which developers will use to interact with.
9
+ #
10
+ # MemcachePod::Client.new(['localhost:11211:10', 'cache-2:11211:5']
11
+ # ,threadsafe => true, :expires_in => 300)
12
+ #
13
+ # This class aims to keep compatiblity ather memcache client.
14
+ #
15
+ def initialize(servers=nil, options={})
16
+ @options = Hash.new
17
+
18
+ @options[:expires_in] = 0
19
+ @options[:threadsafe] = true
20
+ @options[:namespace] = ''
21
+
22
+ if options.has_key?(:expires_in)
23
+ @options[:expires_in] = options[:expires_in]
24
+ end
25
+ if options.has_key?(:threadsafe)
26
+ @options[:threadsafe] = options[:threadsafe]
27
+ end
28
+ if options.has_key?(:namespace)
29
+ @options[:namespace] = options[:namespace]
30
+ end
31
+
32
+ @memory = Memory.new
33
+ end
34
+
35
+ def get(key, options=nil)
36
+ key = validate_key(key)
37
+ return @memory.get(key, options)
38
+ end
39
+
40
+ def set(key, value, ttl=nil, options=nil)
41
+ key = validate_key(key)
42
+ ttl = ttl_or_default(ttl)
43
+ @memory.set(key, value, ttl, options)
44
+ end
45
+
46
+ def add(key, value, ttl=nil, options=nil)
47
+ raise NotImplementedError.new("not implemented add()")
48
+ end
49
+
50
+ def replace(key, value, ttl=nil, options=nil)
51
+ raise NotImplementedError.new("not implemented replace()")
52
+ end
53
+
54
+ def delete(key)
55
+ raise NotImplementedError.new("not implemented delete()")
56
+ end
57
+
58
+ def append(key, value)
59
+ raise NotImplementedError.new("not implemented append()")
60
+ end
61
+
62
+ def prepend(key, value)
63
+ raise NotImplementedError.new("not implemented prepend()")
64
+ end
65
+
66
+ def flush(delay=0)
67
+ @memory.flush()
68
+ end
69
+
70
+ def get_status
71
+ return @memory.get_status
72
+ end
73
+
74
+ alias_method :flush_all, :flush
75
+
76
+ def get_options
77
+ return @options
78
+ end
79
+
80
+ private
81
+
82
+ def validate_key(key)
83
+ raise ArgumentError, "key cannot be blank" if !key || key.length == 0
84
+ key = key_with_namespace(key)
85
+ return key
86
+ end
87
+
88
+ def key_with_namespace(key)
89
+ # TODO: implement namespace
90
+ return key
91
+ end
92
+
93
+ def ttl_or_default(ttl)
94
+ if ttl
95
+ ttl
96
+ else
97
+ @options[:expires_in].to_i
98
+ end
99
+ end
100
+
101
+ end
102
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ # encoding: ascii
4
+ module MemcachePod
5
+ class Memory
6
+
7
+ def initialize()
8
+ @size = 1024*1024 # bytes
9
+ @memory_pool = Hash.new
10
+ end
11
+
12
+ def get(key, options=nil)
13
+ remove_expires()
14
+ if @memory_pool.has_key?(key)
15
+ return @memory_pool[key].body
16
+ else
17
+ return nil
18
+ end
19
+ end
20
+
21
+ def set(key, value, ttl=0, options=nil)
22
+ if(@memory_pool.has_key?(key))
23
+ @memory_pool[key].update(value,ttl)
24
+ else
25
+ @memory_pool[key] = MemoryEntry.new(value,ttl)
26
+ end
27
+ end
28
+
29
+ def flush()
30
+ @memory_pool = Hash.new
31
+ end
32
+
33
+ def get_status
34
+ return @memory_pool.map {|k,v|
35
+ v.get_status
36
+ }
37
+ end
38
+
39
+ private
40
+
41
+ def remove_expires()
42
+ @memory_pool.each_key{ |key|
43
+ entry = @memory_pool[key]
44
+ if not entry.reference?
45
+ if entry.expired?
46
+ @memory_pool.delete(key)
47
+ end
48
+ end
49
+ }
50
+ end
51
+
52
+ end
53
+ end
54
+
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ # encoding: ascii
6
+ module MemcachePod
7
+ class MemoryEntry
8
+
9
+ def initialize(body,ttl)
10
+ @reference = false
11
+ @body = body
12
+ @expires_in = expires_in(ttl)
13
+ end
14
+
15
+ def update(body,ttl)
16
+ @body = body
17
+ @expires_in = expires_in(ttl)
18
+ end
19
+
20
+ def body()
21
+ @reference = true
22
+ return @body
23
+ end
24
+
25
+ def reference?()
26
+ return @refernece
27
+ end
28
+
29
+ def expired?()
30
+ if 0 < @expires_in
31
+ now = Time.now.to_i #second
32
+ return @expires_in < now
33
+ else
34
+ return false
35
+ end
36
+ end
37
+
38
+ def get_status
39
+ exp = 0
40
+ if 0 < @expires_in
41
+ exp = @expires_in - Time.now.to_i
42
+ end
43
+ return {
44
+ 'reference' => @reference,
45
+ 'expires_in' => exp,
46
+ 'bodysize' => @body.size
47
+ }
48
+ end
49
+
50
+ private
51
+
52
+ def expires_in(ttl)
53
+ if 0 < ttl
54
+ now = Time.now.to_i #second
55
+ return now + ttl
56
+ end
57
+ return 0
58
+ end
59
+
60
+ end
61
+ end
62
+
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module MemcachePod
3
+ VERSION = '0.0.1'
4
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'memcachepod/memory_entry'
4
+ require 'memcachepod/memory'
5
+ require 'memcachepod/client'
6
+
7
+ module MemcachePod
8
+
9
+ def self.logger
10
+ @logger = default_logger
11
+ end
12
+
13
+ def self.default_logger
14
+ require 'logger'
15
+ logger = Logger.new(STDOUT)
16
+ logger.level = Logger::INFO
17
+ logger
18
+ end
19
+
20
+ def self.logger=(logger)
21
+ @logger = logger
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memcachepod
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kiyoka Nishiyama
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - kiyoka@sumibi.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - Gemfile
49
+ - LICENSE
50
+ - README.md
51
+ - lib/memcachepod.rb
52
+ - lib/memcachepod/client.rb
53
+ - lib/memcachepod/memory.rb
54
+ - lib/memcachepod/memory_entry.rb
55
+ - lib/memcachepod/version.rb
56
+ homepage: https://github.com/kiyoka/memcachepod
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options:
62
+ - "--charset=UTF-8"
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.6.11
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: ''
81
+ test_files: []