gcloud-datastore-activesupport 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8a6e05b5a4b0d7c14338eda2b9c4c9c6d7c024b474dc25b74b442a0310be86a7
4
+ data.tar.gz: 5907ca3f944d9a1b6cbde7005c57402b8127f658971452bbe5caad4783e9cb0d
5
+ SHA512:
6
+ metadata.gz: 0cbc0fc85ff48978e78df5100c2ed2929c4f076932be7a7f438aba64741e297f4c5bed42f26c983274dcc82d0b765fee68c1ee18230c56496058d62a1152e191
7
+ data.tar.gz: 0a0f39cb67c530ac436337d72858761d427f2db8f318c1336f2b009ba95a78e708d85c4b2eeba4d2f7ebb345f2b3e76b5ea3055dc82b17d9629927deb87923e2
@@ -0,0 +1,20 @@
1
+ Copyright 2019 LeleRed
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # Gcloud::Datastore::Activesupport
2
+ Use Google Datastore as Cache for Ruby on Rails application.
3
+
4
+ ## Usage
5
+ How to use the plugin:
6
+
7
+ ```ruby
8
+
9
+ config.cache_store = :gcloud_datastore
10
+
11
+ ```
12
+ Enviroment variable GOOGLE_CLOUD_PROJECT required.
13
+
14
+
15
+ Alternately:
16
+
17
+ ```ruby
18
+
19
+ config.cache_store = :gcloud_datastore, {
20
+ project_id: "gcloud-project-slug-name"
21
+ }
22
+
23
+ ```
24
+
25
+ Sometimes credential file is needed (for example in local development):
26
+
27
+ ```ruby
28
+
29
+ config.cache_store = :gcloud_datastore, {
30
+ project_id: "gcloud-project-slug-name",
31
+ credential_file_json: "/path/to/credential_file.json"
32
+ }
33
+
34
+ ```
35
+
36
+ or set enviroment variable: GOOGLE_APPLICATION_CREDENTIALS
37
+ ```bash
38
+ export GOOGLE_APPLICATION_CREDENTIALS /path/to/credential_file.json
39
+ ```
40
+
41
+ ## Credential file configuration
42
+
43
+ https://developers.google.com/accounts/docs/application-default-credentials
44
+
45
+ https://console.cloud.google.com/apis/credentials/serviceaccountkey
46
+
47
+
48
+ ## Installation
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'gcloud-datastore-activesupport'
53
+ ```
54
+
55
+ And then execute:
56
+ ```bash
57
+ $ bundle
58
+ ```
59
+
60
+ Or install it yourself as:
61
+ ```bash
62
+ $ gem install gcloud-datastore-activesupport
63
+ ```
64
+
65
+ ## Contributing
66
+ Contribution directions go here.
67
+
68
+ ## License
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Gcloud::Datastore::Activesupport'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,88 @@
1
+ # encoding: UTF-8
2
+ require "google/cloud/datastore"
3
+ require "zlib"
4
+
5
+
6
+ module ActiveSupport
7
+ module Cache
8
+ class GcloudDatastore < Store
9
+
10
+ # attr_reader :project_id
11
+
12
+ def initialize(*options)
13
+ super(options)
14
+ @options = options.extract_options!
15
+ @project_id = @options[:project_id] || Rails.application.config.database_configuration[Rails.env]["dataset_id"]
16
+ @credential_file_json = @options[:credential_file_json]
17
+ @serialize_content_zip_enabled = (@options[:content_zip].blank? || @options[:content_zip]) ? true : false
18
+ @serialize_content_base64_enabled = (@options[:content_base64].blank? || @options[:content_base64]) ? true : false
19
+ @datastore_kind_name = @options[:kind_name] || "cache_#{ Rails.env }"
20
+ end
21
+
22
+
23
+ private
24
+
25
+ # Reads an entry from the cache implementation. Subclasses must implement
26
+ # this method.
27
+ def read_entry(key, options)
28
+ query = Google::Cloud::Datastore::Key.new @datastore_kind_name, key
29
+ entities = dataset.lookup query
30
+ return nil unless entities.any?
31
+ entity = entities.first
32
+ entry = deserialize_entry( entity[:_value], entity[:_format] )
33
+ return entry
34
+ end
35
+
36
+ # Writes an entry to the cache implementation. Subclasses must implement
37
+ # this method.
38
+ def write_entry(key, entry, options)
39
+ value, format = serialize_entry( entry )
40
+ obj = dataset.entity @datastore_kind_name, key do |t|
41
+ t["_expires"] = entry.expires_at
42
+ t["_format"] = format
43
+ t["_value"] = value
44
+ t["_json"] = entry.to_json unless Rails.env.production? || entry.to_json.size >= 1500
45
+ end
46
+ dataset.save obj
47
+ return true
48
+ rescue Exception => e
49
+ return false
50
+ end
51
+
52
+ # Deletes an entry from the cache implementation. Subclasses must
53
+ # implement this method.
54
+ def delete_entry(key, options)
55
+ dataset.delete Google::Cloud::Datastore::Key.new @datastore_kind_name, key
56
+ end
57
+
58
+ def dataset
59
+ @dataset ||= Google::Cloud::Datastore.new(
60
+ project_id: @project_id,
61
+ credentials: @credential_file_json.blank? ? nil : Google::Cloud::Datastore::Credentials.new( @credential_file_json )
62
+ )
63
+ end
64
+
65
+ def serialize_entry( entry )
66
+ format = []
67
+ entry = Marshal.dump( entry )
68
+ if @serialize_content_zip_enabled
69
+ entry = Zlib::Deflate.deflate( entry )
70
+ format << "zip"
71
+ end
72
+ if @serialize_content_base64_enabled
73
+ entry = Base64.encode64( entry ) if @serialize_content_base64_enabled
74
+ format << "base64"
75
+ end
76
+ return entry, format.join("_")
77
+ end
78
+
79
+ def deserialize_entry( entry, format = nil )
80
+ format ||= "zip_base64"
81
+ entry = Base64.decode64( entry ) if format.include? "base64"
82
+ entry = Zlib::Inflate.inflate( entry ) if format.include? "zip"
83
+ Marshal.load( entry )
84
+ end
85
+
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,9 @@
1
+ require "gcloud/datastore/activesupport/railtie"
2
+
3
+ module Gcloud
4
+ module Datastore
5
+ module Activesupport
6
+ # Your code goes here...
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Gcloud
2
+ module Datastore
3
+ module Activesupport
4
+ class Railtie < ::Rails::Railtie
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Gcloud
2
+ module Datastore
3
+ module Activesupport
4
+ VERSION = '0.1.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :gcloud_datastore_activesupport do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gcloud-datastore-activesupport
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - LeleRed
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-06-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-cloud-datastore
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: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'Gcloud::Datastore::Activesupport: Google Datastore as Cache for Ruby
56
+ on Rails application'
57
+ email:
58
+ - lelered@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - MIT-LICENSE
64
+ - README.md
65
+ - Rakefile
66
+ - lib/active_support/cache/gcloud_datastore.rb
67
+ - lib/gcloud/datastore/activesupport.rb
68
+ - lib/gcloud/datastore/activesupport/railtie.rb
69
+ - lib/gcloud/datastore/activesupport/version.rb
70
+ - lib/tasks/gcloud/datastore/activesupport_tasks.rake
71
+ homepage: https://github.com/lelered/gcloud-datastore-activesupport
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ allowed_push_host: https://rubygems.org
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.7.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Google Datastore as Cache for Ruby on Rails application.
96
+ test_files: []