hikki-memcache 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4eeca4aa6f85ac7d84cd5ef20bc244fef02f8001
4
+ data.tar.gz: acc07c8da2def0def1526533f739b3cd012ec86c
5
+ SHA512:
6
+ metadata.gz: dde8e64cf59e8a0ec665072bb71d1ca9bcaaf061591384522a9b6adb1d07b4d45da886829d02326b07d7afa1c7e3fe3099403140fdef4c8693c4d775c3bb16b6
7
+ data.tar.gz: 15d40b433992c40b402008052f1ce7187486646588a25d59a6eeacf3d6d9a3618c08bf10aa843d33a17c3d2f990ce36693d0f5c4d1ad70ecea412ce4004f8e85
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hikki-memcache.gemspec
4
+ gemspec
5
+
6
+ # Grab Hikki from local relative
7
+ gem 'hikki', path: '../../'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 alexpeachey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # Hikki::Adapters::MemcacheAdapter
2
+
3
+ A Memcache adapter for Hikki.
4
+ It uses the `dalli` gem to communicate with Memcache.
5
+ Depending on expiry options and size of memory, all objects are not guaranteed to be available.
6
+ As such, `index`, `all`, and `find_by` are not implemented.
7
+ The `index` method will just return `false` if called.
8
+ The `all` and `find_by` will return `[]`. Due to the repository logic, if this adapter is your first
9
+ adapter then calling `all` on the repository will immediately try the next reader in the list.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'hikki-memcache'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install hikki-memcache
24
+
25
+ ## Usage
26
+
27
+ By default, the adapter will use `Dalli::Client.new('localhost:11211', { namespace: 'hikki', compress: true })` as
28
+ it's connection.
29
+ You can pass in your own connection to use instead, useful for specifying a server, namespace and other options.
30
+
31
+ ```ruby
32
+ # Use the default connection
33
+ adapter = Hikki::Adapters::MemcacheAdapter.new
34
+
35
+ # Use a specific connection
36
+ cache = Dalli::Client.new('cache.example.com:11211', { namespace: 'my_cache' })
37
+ adapter = Hikki::Adapters::MemcacheAdapter.new(cache)
38
+ ```
39
+
40
+ If you do not specify an `id` when saving, the adapter will generate a uuid using `SecureRandom`.
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( http://github.com/originate/hikki/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Write your specifications
47
+ 4. Implement your specifications
48
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 6. Push to the branch (`git push origin my-new-feature`)
50
+ 7. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require File.expand_path('../../../lib/hikki/version', __FILE__)
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hikki-memcache"
8
+ spec.version = Hikki::VERSION
9
+ spec.authors = ["alexpeachey"]
10
+ spec.email = ["alex.peachey@originate.com"]
11
+ spec.summary = 'A Memcache adapter for Hikki.'
12
+ spec.description = 'A Memcache adapter for Hikki.'
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'hikki', Hikki::VERSION
22
+ spec.add_dependency 'dalli'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,22 @@
1
+ require 'dalli'
2
+ require 'hikki'
3
+ require_relative './memcache_collection'
4
+
5
+ module Hikki
6
+ module Adapters
7
+ class MemcacheAdapter < Hikki::Adapters::Adapter
8
+ attr_reader :connection, :uuid_generator
9
+
10
+ def initialize(connection=Dalli::Client.new('localhost:11211', { namespace: 'hikki', compress: true }), uuid_generator=SecureRandom)
11
+ super()
12
+ @connection = connection
13
+ @uuid_generator = uuid_generator
14
+ end
15
+
16
+ def collection_for(collection)
17
+ collections.fetch(collection, MemcacheCollection.new(collection, connection, uuid_generator))
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,54 @@
1
+ module Hikki
2
+ module Adapters
3
+ class MemcacheCollection < Hikki::Collection
4
+ attr_reader :connection, :uuid_generator
5
+
6
+ def initialize(collection, connection, uuid_generator)
7
+ super(collection)
8
+ @connection = connection
9
+ @uuid_generator = uuid_generator
10
+ end
11
+
12
+ def index(field)
13
+ false
14
+ end
15
+
16
+ def save(data)
17
+ data = normalize_data(data)
18
+ connection.set(key(data['id']), data)
19
+ data
20
+ end
21
+
22
+ def find(id)
23
+ connection.get(key(id)) || {}
24
+ end
25
+
26
+ def all(options={})
27
+ []
28
+ end
29
+
30
+ def find_by(field, value, options={})
31
+ []
32
+ end
33
+
34
+ def remove(id)
35
+ connection.delete(key(id))
36
+ true
37
+ end
38
+
39
+ def remove_all
40
+ connection.flush
41
+ end
42
+
43
+ def id_for(data)
44
+ data.fetch('id', uuid_generator.uuid).to_s
45
+ end
46
+
47
+ private
48
+ def key(id)
49
+ collection + ':' + id.to_s
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module Hikki
4
+ module Adapters
5
+ describe MemcacheAdapter, :integration do
6
+ context 'when actually using Memcache' do
7
+ subject(:adapter) { MemcacheAdapter.new }
8
+ let(:data) { { id: id, field1: 'test', field2: 123 } }
9
+ let(:expected) { { 'id' => id, 'field1' => 'test', 'field2' => 123 } }
10
+ let(:id) { '1' }
11
+ let(:collection) { 'collection1' }
12
+
13
+ it 'can store and retreive data' do
14
+ adapter.remove_all(collection)
15
+ adapter.index(collection, :field1)
16
+ adapter.save(collection, data)
17
+ expect(adapter.find(collection, id)).to eq expected
18
+ expect(adapter.find(collection, '2')).to eq Hash.new
19
+ expect(adapter.all(collection)).to eq []
20
+ expect(adapter.find_by(collection, :field1, 'test')).to eq []
21
+ expect(adapter.find_by(collection, :field2, 123)).to eq []
22
+ expect(adapter.find_by(collection, :field1, 'foo')).to eq []
23
+ expect(adapter.find_by(collection, :field2, 1)).to eq []
24
+ adapter.remove(collection, id)
25
+ expect(adapter.find(collection, id)).to eq Hash.new
26
+ expect(adapter.all(collection)).to eq []
27
+ 10.times do |i|
28
+ adapter.save(collection, { id: i, field1: "test-#{i%2}" })
29
+ end
30
+ expect(adapter.all(collection, {limit: 2, offset: 6})).to eq []
31
+ expect(adapter.find_by(collection, :field1, 'test-0', {limit: 2})).to eq []
32
+ adapter.remove_all(collection)
33
+ expect(adapter.find(collection, id)).to eq Hash.new
34
+ expect(adapter.all(collection)).to eq []
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ module Hikki
4
+ module Adapters
5
+ describe MemcacheAdapter do
6
+ subject(:adapter) { MemcacheAdapter.new(connection, uuid_generator) }
7
+ let(:connection) { double :connection, set: true, get: nil, delete: true, flush: true }
8
+ let(:uuid_generator) { double :uuid_generator, uuid: '12345' }
9
+ let(:collection) { 'collection1' }
10
+
11
+ describe '#index' do
12
+ it 'returns false' do
13
+ expect(adapter.index(collection, :field1)).to be_false
14
+ end
15
+ end
16
+
17
+ describe '#save' do
18
+ context 'when an id is provided in the data' do
19
+ let(:data) { { id: id, field1: 'test', field2: 123 } }
20
+ let(:expected) { { 'id' => id, 'field1' => 'test', 'field2' => 123 } }
21
+ let(:id) { '1' }
22
+ let(:key) { collection + ':' + id }
23
+
24
+ it 'returns the data' do
25
+ expect(adapter.save(collection, data)).to eq expected
26
+ end
27
+
28
+ it 'persists the data in the store' do
29
+ adapter.save(collection, data)
30
+ expect(connection).to have_received(:set).with(key, expected)
31
+ end
32
+ end
33
+
34
+ context 'when an id is not provided in the data' do
35
+ let(:data) { { field1: 'test', field2: 123 } }
36
+ let(:expected) { { 'id' => id, 'field1' => 'test', 'field2' => 123 } }
37
+ let(:id) { '12345' }
38
+ let(:key) { collection + ':' + id }
39
+
40
+ it 'returns the data with the id added' do
41
+ expect(adapter.save(collection, data)).to eq expected
42
+ end
43
+
44
+ it 'persists the data in the store' do
45
+ adapter.save(collection, data)
46
+ expect(connection).to have_received(:set).with(key, expected)
47
+ end
48
+ end
49
+ end
50
+
51
+ describe '#find' do
52
+ let(:id) { '1' }
53
+ let(:key) { collection + ':' + id }
54
+
55
+ context 'when the id exists' do
56
+ let(:expected) { { 'id' => id, 'field1' => 'test', 'field2' => 123 } }
57
+ before { connection.stub(:get).with(key).and_return(expected) }
58
+
59
+ it 'retrieves the data' do
60
+ expect(adapter.find(collection, id)).to eq expected
61
+ end
62
+ end
63
+
64
+ context 'when the id does not exist' do
65
+ let(:expected) { {} }
66
+ before { connection.stub(:get).with(key).and_return(nil) }
67
+
68
+ it 'returns an empty hash' do
69
+ expect(adapter.find(collection, id)).to eq expected
70
+ end
71
+ end
72
+ end
73
+
74
+ describe '#all' do
75
+ it 'returns []' do
76
+ expect(adapter.all(collection)).to eq []
77
+ expect(adapter.all(collection, {})).to eq []
78
+ end
79
+ end
80
+
81
+ describe '#find_by' do
82
+ it 'returns []' do
83
+ expect(adapter.find_by(collection, :field1, 'test')).to eq []
84
+ expect(adapter.find_by(collection, :field1, 'test', {})).to eq []
85
+ end
86
+ end
87
+
88
+ describe '#remove' do
89
+ let(:id) { '1' }
90
+ let(:key) { collection + ':' + id }
91
+
92
+ it 'returns true' do
93
+ expect(adapter.remove(collection, id)).to be_true
94
+ end
95
+
96
+ it 'removes the record from the store' do
97
+ adapter.remove(collection, id)
98
+ expect(connection).to have_received(:delete).with(key)
99
+ end
100
+ end
101
+
102
+ describe '#remove_all' do
103
+ it 'returns true' do
104
+ expect(adapter.remove_all(collection)).to be_true
105
+ end
106
+
107
+ it 'removes all of the records from the store' do
108
+ adapter.remove_all(collection)
109
+ expect(connection).to have_received(:flush)
110
+ end
111
+ end
112
+
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'hikki/adapters/memcache_adapter'
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ config.filter_run_excluding :integration
8
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hikki-memcache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - alexpeachey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hikki
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: dalli
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A Memcache adapter for Hikki.
84
+ email:
85
+ - alex.peachey@originate.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .rspec
91
+ - .travis.yml
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - hikki-memcache.gemspec
97
+ - lib/hikki/adapters/memcache_adapter.rb
98
+ - lib/hikki/adapters/memcache_collection.rb
99
+ - spec/hikki/adapters/memcache_adapter_integration_spec.rb
100
+ - spec/hikki/adapters/memcache_adapter_spec.rb
101
+ - spec/spec_helper.rb
102
+ homepage: ''
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Memcache adapter for Hikki.
126
+ test_files:
127
+ - spec/hikki/adapters/memcache_adapter_integration_spec.rb
128
+ - spec/hikki/adapters/memcache_adapter_spec.rb
129
+ - spec/spec_helper.rb