torquebox-cache 2.0.3-java → 2.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -84,6 +84,10 @@ module ActiveSupport
84
84
 
85
85
  protected
86
86
 
87
+ def defaults
88
+ {:name=>'__torque_box_store__', :mode => :invalidation, :sync => false}
89
+ end
90
+
87
91
  def encode value
88
92
  Marshal.dump(value).to_java_bytes
89
93
  end
@@ -116,7 +120,7 @@ module ActiveSupport
116
120
  private
117
121
 
118
122
  def cache
119
- @cache ||= TorqueBox::Infinispan::Cache.new(options)
123
+ @cache ||= TorqueBox::Infinispan::Cache.new( defaults.merge(options) )
120
124
  end
121
125
  end
122
126
  end
data/lib/cache.rb CHANGED
@@ -18,6 +18,7 @@
18
18
  require 'torquebox/kernel'
19
19
  require 'torquebox/injectors'
20
20
  require 'torquebox/transactions'
21
+ require 'sequence'
21
22
 
22
23
  module TorqueBox
23
24
  module Infinispan
@@ -31,7 +32,7 @@ module TorqueBox
31
32
  end
32
33
 
33
34
  def getTransactionManager
34
- inject('transaction-manager')
35
+ fetch('transaction-manager')
35
36
  end
36
37
  end
37
38
 
@@ -45,40 +46,6 @@ module TorqueBox
45
46
  end
46
47
  end
47
48
 
48
- class Sequence
49
- include java.io.Serializable
50
-
51
- class Codec
52
- def self.encode(sequence)
53
- sequence.value.to_s
54
- end
55
-
56
- def self.decode(sequence_bytes)
57
- sequence_bytes && Sequence.new( sequence_bytes.to_s.to_i )
58
- end
59
- end
60
-
61
- def initialize(amount = 1)
62
- @data = amount
63
- end
64
-
65
- def value
66
- @data ? @data.to_i : @data
67
- end
68
-
69
- def next(amount = 1)
70
- Sequence.new( @data.to_i + amount )
71
- end
72
-
73
- def ==(other)
74
- self.value == other.value
75
- end
76
-
77
- def to_s
78
- "Sequence: #{self.value}"
79
- end
80
- end
81
-
82
49
  class Cache
83
50
  include TorqueBox::Injectors
84
51
 
@@ -87,8 +54,9 @@ module TorqueBox
87
54
  java_import org.infinispan.config.Configuration::CacheMode
88
55
  java_import org.infinispan.transaction::TransactionMode
89
56
  java_import org.infinispan.transaction::LockingMode
57
+ java_import org.projectodd.polyglot.cache.as::CacheService
90
58
  INFINISPAN_AVAILABLE = true
91
- rescue NameError
59
+ rescue NameError => e
92
60
  INFINISPAN_AVAILABLE = false
93
61
  # Not running inside TorqueBox
94
62
  end
@@ -106,21 +74,38 @@ module TorqueBox
106
74
  options[:name] || TORQUEBOX_APP_NAME
107
75
  end
108
76
 
109
- def search_manager
110
- @search_manager ||= org.infinispan.query.Search.getSearchManager(@cache)
77
+ def persisted?
78
+ !options[:persist].nil?
79
+ end
80
+
81
+ def replicated?
82
+ [:r, :repl, :replicated, :replication].include? options[:mode]
83
+ end
84
+
85
+ def distributed?
86
+ [:d, :dist, :distributed, :distribution].include? options[:mode]
87
+ end
88
+
89
+ def invalidated?
90
+ [:i, :inv, :invalidated, :invalidation].include? options[:mode]
91
+ end
92
+
93
+ def clustered?
94
+ INFINISPAN_AVAILABLE && service.clustered?
111
95
  end
112
96
 
113
97
  def clustering_mode
114
- replicated = [:r, :repl, :replicated, :replication].include? options[:mode]
115
- distributed = [:d, :dist, :distributed, :distribution].include? options[:mode]
98
+ return CacheMode::LOCAL unless clustered?
116
99
  sync = options[:sync]
117
100
  case
118
- when replicated
101
+ when replicated?
119
102
  sync ? CacheMode::REPL_SYNC : CacheMode::REPL_ASYNC
120
- when distributed
103
+ when distributed?
121
104
  sync ? CacheMode::DIST_SYNC : CacheMode::DIST_ASYNC
122
- else
105
+ when invalidated?
123
106
  sync ? CacheMode::INVALIDATION_SYNC : CacheMode::INVALIDATION_ASYNC
107
+ else
108
+ sync ? CacheMode::DIST_SYNC : CacheMode::DIST_ASYNC
124
109
  end
125
110
  end
126
111
 
@@ -220,7 +205,7 @@ module TorqueBox
220
205
  def transaction(&block)
221
206
  if !transactional?
222
207
  yield self
223
- elsif inject('transaction-manager').nil?
208
+ elsif fetch('transaction-manager').nil?
224
209
  tm = cache.getAdvancedCache.getTransactionManager
225
210
  begin
226
211
  tm.begin if tm
@@ -258,15 +243,6 @@ module TorqueBox
258
243
  cache.stop
259
244
  end
260
245
 
261
- def self.shutdown
262
- Cache.local_managers.each { |m| m.stop }
263
- end
264
-
265
- @@local_managers = []
266
- def self.local_managers
267
- @@local_managers
268
- end
269
-
270
246
  def self.log( message, status = 'INFO' )
271
247
  $stdout.puts( "#{status}: #{message}" )
272
248
  end
@@ -282,108 +258,62 @@ module TorqueBox
282
258
  end
283
259
 
284
260
  def cache
285
- if INFINISPAN_AVAILABLE
286
- @cache ||= clustered || local || nothing
261
+ if INFINISPAN_AVAILABLE
262
+ @cache ||= manager.running?( name ) ? reconfigure : configure
287
263
  else
288
264
  @cache ||= nothing
289
265
  end
290
266
  end
291
267
 
268
+ def service
269
+ @service ||= TorqueBox::ServiceRegistry[CacheService::CACHE]
270
+ end
271
+
292
272
  def manager
293
- begin
294
- @manager ||= TorqueBox::ServiceRegistry[org.jboss.msc.service.ServiceName::JBOSS.append( "infinispan", "torquebox" )]
295
- rescue Exception => e
296
- log( "Caught exception while looking up Infinispan service.", 'ERROR' )
297
- log( e.message, 'ERROR' )
298
- end
299
- @manager
273
+ @manager ||= service.cache_container
300
274
  end
301
-
275
+
302
276
  def reconfigure(mode=clustering_mode)
303
- cache = manager.get_cache(name)
304
- base_config = cache.configuration
277
+ existing_cache = manager.get_cache(name)
278
+ base_config = existing_cache.configuration
305
279
  unless base_config.cache_mode == mode
306
280
  log( "Reconfiguring Infinispan cache #{name} from #{base_config.cache_mode} to #{mode}" )
307
- cache.stop
308
- base_config.cache_mode = mode
309
- config = base_config.fluent
310
- config.transaction.transactionMode( transaction_mode )
311
- config.transaction.recovery.transactionManagerLookup( transaction_manager_lookup )
312
- manager.define_configuration(name, config.build)
313
- cache.start
281
+ existing_cache .stop
282
+ configure(mode)
283
+ existing_cache .start
314
284
  end
315
- return cache
285
+ return existing_cache
316
286
  end
317
287
 
318
288
  def configure(mode=clustering_mode)
319
289
  log( "Configuring Infinispan cache #{name} as #{mode}" )
320
290
  base_config = manager.default_configuration.clone
321
- base_config.class_loader = java.lang::Thread.current_thread.context_class_loader
322
291
  base_config.cache_mode = mode
323
-
324
292
  config = base_config.fluent
325
293
  config.transaction.transactionMode( transaction_mode )
326
- config.transaction.recovery.transactionManagerLookup( transaction_manager_lookup )
327
- manager.define_configuration(name, config.build )
328
- manager.get_cache(name)
329
- end
330
-
331
- def transaction_manager_lookup
332
- @tm ||= if inject('transaction-manager')
333
- ContainerTransactionManagerLookup.new
334
- else
335
- org.infinispan.transaction.lookup.GenericTransactionManagerLookup.new
336
- end
337
- end
338
-
339
- def clustered
340
- (manager.running?(name) ? reconfigure : configure) if manager
341
- rescue
342
- log( "Clustered: Can't get clustered cache; falling back to local: #{$!}", 'ERROR' )
343
- end
344
-
345
- def local
346
- log( "Configuring Infinispan local cache #{name}" )
347
- bare_config = org.infinispan.config.Configuration.new
348
- bare_config.class_loader = java.lang::Thread.current_thread.context_class_loader
349
-
350
- config = bare_config.fluent
351
- config.transaction.transactionMode( transaction_mode )
352
-
353
294
  if transactional?
354
295
  config.transaction.transactionManagerLookup( transaction_manager_lookup )
355
296
  config.transaction.lockingMode( locking_mode )
356
297
  end
357
-
358
- if options[:persist]
359
- log( "Configuring #{name} local cache for file-based persistence" )
298
+ if persisted?
360
299
  store = org.infinispan.loaders.file.FileCacheStoreConfig.new
361
300
  store.purge_on_startup( false )
362
301
  store.location(options[:persist]) if File.exist?( options[:persist].to_s )
363
302
  config.loaders.add_cache_loader( store )
364
303
  end
304
+ manager.define_configuration(name, config.build )
305
+ manager.get_cache(name)
306
+ end
365
307
 
366
- if options[:index]
367
- log( "Configuring #{name} local cache for local-only indexing" )
368
- config.indexing.index_local_only(true)
369
- else
370
- log( "Configuring #{name} local cache with no indexing" )
371
- config.indexing.disable
372
- end
373
-
374
- if ((local_manager = Cache.find_local_manager(name)) == nil)
375
- log( "No local CacheManager exists for #{name}. Creating one." )
376
- local_manager = org.infinispan.manager.DefaultCacheManager.new
377
- local_manager.define_configuration( name, config.build )
378
- Cache.local_managers << local_manager
379
- end
380
-
381
- local_manager.get_cache( self.name )
382
- rescue Exception => e
383
- log( "Unable to obtain local cache: #{$!}", 'ERROR' )
384
- log( e.backtrace, 'ERROR' )
308
+ def transaction_manager_lookup
309
+ @tm ||= if fetch('transaction-manager')
310
+ ContainerTransactionManagerLookup.new
311
+ else
312
+ org.infinispan.transaction.lookup.GenericTransactionManagerLookup.new
313
+ end
385
314
  end
386
315
 
316
+
387
317
  def nothing
388
318
  result = Object.new
389
319
  def result.method_missing(*args); end
@@ -401,25 +331,11 @@ module TorqueBox
401
331
  args << expires_in << SECONDS
402
332
  args << expires << SECONDS
403
333
  end
404
- #$stderr.puts "cache=#{cache.inspect}"
405
- #$stderr.puts "*args=#{args.inspect}"
406
334
  cache.send( *args ) && true
407
335
  end
408
336
 
409
- # Finds the CacheManager for a given cache and returns it - or nil if not found
410
- def self.find_local_manager( cache_name )
411
- local_managers.each do |m|
412
- if m.cache_exists( cache_name )
413
- log( ":-:-:-: local_manager already exists for #{cache_name}" )
414
- return m
415
- end
416
- end
417
- return nil
418
- end
419
-
420
337
  end
421
338
 
422
- at_exit { Cache.shutdown }
423
339
  end
424
340
  end
425
341
 
@@ -15,6 +15,8 @@
15
15
  # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
16
  # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
17
 
18
+ require 'jruby/core_ext'
19
+
18
20
  module TorqueBox
19
21
  module Infinispan
20
22
  class CacheListener
@@ -28,7 +30,11 @@ module TorqueBox
28
30
  end
29
31
  end
30
32
 
31
- add_class_annotation( { org.infinispan.notifications.Listener => { } } )
33
+ if JRUBY_VERSION =~ /^1\.7/
34
+ add_class_annotations( { org.infinispan.notifications.Listener => { } } )
35
+ else
36
+ add_class_annotation( { org.infinispan.notifications.Listener => { } } )
37
+ end
32
38
  add_method_signature( "event_fired", [java.lang.Void::TYPE, org.infinispan.notifications.cachelistener.event.Event] )
33
39
  add_method_annotation( "event_fired",
34
40
  { org.infinispan.notifications.cachelistener.annotation.CacheEntryCreated => {},
data/lib/gem_hook.rb CHANGED
@@ -16,7 +16,10 @@
16
16
  # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
17
 
18
18
  require 'cache'
19
- #require 'active_support/cache/torque_box_store.rb'
20
- #require 'datamapper/dm-infinispan-adapter'
21
-
19
+ begin
20
+ ActiveSupport
21
+ require 'active_support/cache/torque_box_store.rb'
22
+ rescue NameError
23
+ # ActiveSupport not used
24
+ end
22
25
 
Binary file
Binary file
@@ -1,4 +1,4 @@
1
- # Copyright 2008-2012 Red Hat, Inc, and individual contributors.
1
+ # Copyright 2012 Red Hat, Inc, and individual contributors.
2
2
  #
3
3
  # This is free software; you can redistribute it and/or modify it
4
4
  # under the terms of the GNU Lesser General Public License as
@@ -15,4 +15,44 @@
15
15
  # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16
16
  # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
17
17
 
18
- require 'datamapper/dm-infinispan-adapter'
18
+ module TorqueBox
19
+ module Infinispan
20
+
21
+ class Sequence
22
+ include java.io.Serializable
23
+
24
+ class Codec
25
+ def self.encode(sequence)
26
+ sequence.value.to_s
27
+ end
28
+
29
+ def self.decode(sequence_bytes)
30
+ sequence_bytes && Sequence.new( sequence_bytes.to_s.to_i )
31
+ end
32
+ end
33
+
34
+ def initialize(amount = 1)
35
+ @data = amount
36
+ end
37
+
38
+ def value
39
+ @data ? @data.to_i : @data
40
+ end
41
+
42
+ def next(amount = 1)
43
+ Sequence.new( @data.to_i + amount )
44
+ end
45
+
46
+ def ==(other)
47
+ self.value == other.value
48
+ end
49
+
50
+ def to_s
51
+ "Sequence: #{self.value}"
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+
58
+
Binary file
@@ -1,10 +1,12 @@
1
1
  module TorqueboxCache
2
- VERSION = '2.0.3'
3
- MAVEN_VERSION = '2.0.3'
2
+ VERSION = '2.1.0'
3
+ MAVEN_VERSION = '2.1.0'
4
4
  end
5
5
  begin
6
6
  require 'java'
7
7
  require File.dirname(__FILE__) + '/torquebox-cache.jar'
8
+ require File.dirname(__FILE__) + '/polyglot-cache-1.7.1.jar'
9
+ require File.dirname(__FILE__) + '/polyglot-core-1.7.1.jar'
8
10
  rescue LoadError
9
11
  puts 'JAR-based gems require JRuby to load. Please visit www.jruby.org.'
10
12
  raise
@@ -19,6 +19,10 @@ require 'cache_listener'
19
19
 
20
20
  describe TorqueBox::Infinispan::CacheListener do
21
21
  before :each do
22
+ manager = org.infinispan.manager.DefaultCacheManager.new
23
+ service = org.projectodd.polyglot.cache.as.CacheService.new
24
+ service.stub!(:cache_container).and_return( manager )
25
+ TorqueBox::ServiceRegistry.stub!(:[]).with(org.projectodd.polyglot.cache.as.CacheService::CACHE).and_return( service )
22
26
  @cache = TorqueBox::Infinispan::Cache.new( :name => 'foo-cache' )
23
27
  end
24
28
 
data/spec/cache_spec.rb CHANGED
@@ -18,6 +18,10 @@ require File.dirname(__FILE__) + '/spec_helper'
18
18
 
19
19
  describe TorqueBox::Infinispan::Cache do
20
20
  before :each do
21
+ manager = org.infinispan.manager.DefaultCacheManager.new
22
+ service = org.projectodd.polyglot.cache.as.CacheService.new
23
+ service.stub!(:cache_container).and_return( manager )
24
+ TorqueBox::ServiceRegistry.stub!(:[]).with(org.projectodd.polyglot.cache.as.CacheService::CACHE).and_return( service )
21
25
  @cache = TorqueBox::Infinispan::Cache.new( :name => 'foo-cache' )
22
26
  end
23
27
 
@@ -29,11 +33,6 @@ describe TorqueBox::Infinispan::Cache do
29
33
  @cache.name.should == 'foo-cache'
30
34
  end
31
35
 
32
- it "should reuse existing cache managers for an extant local cache" do
33
- TorqueBox::Infinispan::Cache.should_receive( :find_local_manager ).with( 'foo-cache' )
34
- TorqueBox::Infinispan::Cache.new( :name => 'foo-cache' )
35
- end
36
-
37
36
  it "should respond to clustering_mode" do
38
37
  @cache.should respond_to( :clustering_mode )
39
38
  end
@@ -127,9 +126,7 @@ describe TorqueBox::Infinispan::Cache do
127
126
  end
128
127
 
129
128
  it "should increment a sequence" do
130
- puts "AAAA"
131
129
  @cache.increment("My Sequence Name", 1).should == 1
132
- puts "BBBB"
133
130
  @cache.increment("My Sequence Name", 1).should == 2
134
131
  end
135
132
 
@@ -144,6 +141,37 @@ describe TorqueBox::Infinispan::Cache do
144
141
  @cache.get('a false value').should be_false
145
142
  end
146
143
 
144
+ it "should allow symbols as keys for basic put" do
145
+ @cache.put(:asymbol, "a value")
146
+ @cache.get(:asymbol).should == "a value"
147
+ end
148
+
149
+ it "should allow symbols as keys for put_if_absent" do
150
+ @cache.put_if_absent(:asymbol, "a value")
151
+ @cache.get(:asymbol).should == "a value"
152
+ end
153
+
154
+ it "should allow symbols as keys for increment" do
155
+ @cache.increment :countsymbol
156
+ @cache.get(:countsymbol).should == "1"
157
+ @cache.increment(:countsymbol).should == 2
158
+ end
159
+
160
+ it "should store and retrieve zero" do
161
+ @cache.put(:mynumber, 0)
162
+ @cache.get(:mynumber).should == 0
163
+ end
164
+
165
+ it "should store and retrieve integers" do
166
+ @cache.put(:mynumber, 30)
167
+ @cache.get(:mynumber).should == 30
168
+ end
169
+
170
+ it "should store and retrieve floats" do
171
+ @cache.put(:mynumber, 1.0)
172
+ @cache.get(:mynumber).should == 1.0
173
+ end
174
+
147
175
  it "should expire entries based on provided expiry durations" do
148
176
  cache = TorqueBox::Infinispan::Cache.new( :name => 'expiring-cache' )
149
177
  cache.put("foo", "bar", 0.1)
@@ -323,21 +351,6 @@ describe TorqueBox::Infinispan::Cache do
323
351
  end
324
352
  end
325
353
 
326
- describe "with search" do
327
- before :each do
328
- @cache = TorqueBox::Infinispan::Cache.new( :name => 'foo-cache' )
329
- end
330
-
331
- it "should ask the cache for the search managager" do
332
- @cache.should_receive :search_manager
333
- Infinispan::Search.new(@cache, lambda{|v|v})
334
- end
335
-
336
- after :each do
337
- @cache.clear
338
- end
339
- end
340
-
341
354
  end
342
355
 
343
356
  class Snuffleuffagus