db-charmer 1.6.11 → 1.6.12

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,12 @@
1
+ 1.6.12 (2010-05-09):
2
+
3
+ Starting with this version we use Rails.cache (memcache or whatever you use in your project)
4
+ to cache sharding blocks information.
5
+
6
+ Bugfixes: Thanks to Allen Madsen (github user blatyo) we've fixed a few minor issues in
7
+ database connections handling.
8
+
9
+ ----------------------------------------------------------------------------------------
1
10
  1.6.11 (2010-04-16):
2
11
 
3
12
  Bugfix: Change the way we allocate sharding blocks in block map sharding method to
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.6.11
1
+ 1.6.12
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{db-charmer}
8
- s.version = "1.6.11"
8
+ s.version = "1.6.12"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexey Kovyrin"]
12
- s.date = %q{2010-04-16}
12
+ s.date = %q{2010-05-09}
13
13
  s.description = %q{ActiveRecord Connections Magic (slaves, multiple connections, etc)}
14
14
  s.email = %q{alexey@kovyrin.net}
15
15
  s.extra_rdoc_files = [
@@ -10,6 +10,10 @@ module DbCharmer
10
10
  @db_name
11
11
  end
12
12
 
13
+ def db_charmer_connection_proxy
14
+ self
15
+ end
16
+
13
17
  def method_missing(meth, *args, &block)
14
18
  @abstract_connection_class.retrieve_connection.send(meth, *args, &block)
15
19
  end
@@ -10,8 +10,8 @@ module DbCharmer
10
10
 
11
11
  if conn.kind_of?(Hash)
12
12
  conn = conn.symbolize_keys
13
- raise ArgumentError, "Missing required :name parameter" unless conn[:name]
14
- return DbCharmer::ConnectionFactory.connect_to_db(conn[:name], conn)
13
+ raise ArgumentError, "Missing required :connection_name parameter" unless conn[:connection_name]
14
+ return DbCharmer::ConnectionFactory.connect_to_db(conn[:connection_name], conn)
15
15
  end
16
16
 
17
17
  if conn.respond_to?(:db_charmer_connection_proxy)
@@ -6,7 +6,7 @@ module DbCharmer
6
6
  hijack_connection!
7
7
 
8
8
  # Should requested connections exist in the config?
9
- should_exist = opt[:should_exist] || DbCharmer.connections_should_exist?
9
+ should_exist = opt.has_key?(:should_exist) ? opt[:should_exist] : DbCharmer.connections_should_exist?
10
10
 
11
11
  # Main connection management
12
12
  setup_connection_magic(opt[:connection], should_exist)
@@ -30,7 +30,7 @@ module DbCharmer
30
30
  end
31
31
 
32
32
  def on_db(db_name)
33
- name = db_name.is_a?(Hash) ? db_name[:name] : db_name.inspect
33
+ name = db_name.is_a?(Hash) ? db_name[:connection_name] : db_name.inspect
34
34
  announce "Switching connection to #{name}"
35
35
  # Switch connection
36
36
  old_proxy = ActiveRecord::Base.db_charmer_connection_proxy
@@ -31,7 +31,9 @@ module DbCharmer
31
31
 
32
32
  # Local caches
33
33
  @shard_info_cache = {}
34
- @blocks_cache = {}
34
+
35
+ @blocks_cache = Rails.cache
36
+ @blocks_cache_prefix = config[:blocks_cache_prefix] || "#{@name}_block:"
35
37
  end
36
38
 
37
39
  def shard_for_key(key)
@@ -69,13 +71,29 @@ module DbCharmer
69
71
  # Cleanup the cache if asked to
70
72
  key_range = [ block_start_for_key(key), block_end_for_key(key) ]
71
73
  block_cache_key = "%d-%d" % key_range
72
- @blocks_cache[block_cache_key] = nil unless cache
74
+
75
+ if cache
76
+ cached_block = get_cached_block(block_cache_key)
77
+ return cached_block if cached_block
78
+ end
73
79
 
74
80
  # Fetch cached value or load from db
75
- @blocks_cache[block_cache_key] ||= begin
81
+ block = begin
76
82
  sql = "SELECT * FROM #{map_table} WHERE start_id = #{key_range.first} AND end_id = #{key_range.last} LIMIT 1"
77
83
  connection.select_one(sql, 'Find a shard block')
78
84
  end
85
+
86
+ set_cached_block(block_cache_key, block)
87
+
88
+ return block
89
+ end
90
+
91
+ def get_cached_block(block_cache_key)
92
+ @blocks_cache.read("#{@blocks_cache_prefix}#{block_cache_key}")
93
+ end
94
+
95
+ def set_cached_block(block_cache_key, block)
96
+ @blocks_cache.write("#{@blocks_cache_prefix}#{block_cache_key}", block)
79
97
  end
80
98
 
81
99
  # Load shard info
@@ -144,7 +162,7 @@ module DbCharmer
144
162
  # FIXME: Find a better way, maybe move config method to our ar extenstions
145
163
  connection.instance_variable_get(:@config).clone.merge(
146
164
  # Name for the connection factory
147
- :name => shard_name,
165
+ :connection_name => shard_name,
148
166
  # Connection params
149
167
  :host => shard.db_host,
150
168
  :port => shard.db_port,
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 6
8
- - 11
9
- version: 1.6.11
8
+ - 12
9
+ version: 1.6.12
10
10
  platform: ruby
11
11
  authors:
12
12
  - Alexey Kovyrin
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-16 00:00:00 -04:00
17
+ date: 2010-05-09 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency