Class: TorqueBox::Infinispan::Cache

Inherits:
Object
  • Object
show all
Defined in:
cache/lib/torquebox/cache.rb

Constant Summary

SECONDS =
java.util.concurrent.TimeUnit::SECONDS

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Cache) initialize(opts = {})

Returns a new instance of Cache



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'cache/lib/torquebox/cache.rb', line 45

def initialize(opts = {})
  @options = opts
  
  if INFINISPAN_AVAILABLE
    options[:transaction_mode] = :transactional unless options.has_key?( :transaction_mode )
    options[:locking_mode] ||= :optimistic if (transactional? && !options.has_key?( :locking_mode ))
    options[:sync] = true if options[:sync].nil?
  end

  if options[:encoding] == :marshal
    log( "Encoding of :marshal cannot be used with " +
         "TorqueBox::Infinispan::Cache - using :marshal_base64 instead",
         'WARN')
    options[:encoding] = :marshal_base64
  end
  @codec = TorqueBox::Codecs[ options[:encoding] || :marshal_smart ]
  cache
end

Class Method Details

+ (Object) log(message, status = 'INFO')



242
243
244
# File 'cache/lib/torquebox/cache.rb', line 242

def self.log( message, status = 'INFO' )
  $stdout.puts( "#{status}: #{message}" )
end

Instance Method Details

- (Object) __put(key, value, expires, operation)



368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'cache/lib/torquebox/cache.rb', line 368

def __put(key, value, expires, operation)
  # encode
  args = [ operation, encode(key), encode(value) ]
  if expires > 0
    # Set the Infinispan expire a few minutes into the future to support
    # :race_condition_ttl on read
    #expires_in = expires + 300 # 300 seconds == 5 minutes
    expires_in = expires
    args << expires_in << SECONDS
    args << expires << SECONDS
  end
  decode(cache.send(*args))
end

- (Object) add_listener(listener)



234
235
236
# File 'cache/lib/torquebox/cache.rb', line 234

def add_listener( listener )
  cache.add_listener( listener )
end

- (Object) all



145
146
147
# File 'cache/lib/torquebox/cache.rb', line 145

def all
  keys.map{|k| get(k)}
end

- (Object) cache



264
265
266
267
268
269
270
# File 'cache/lib/torquebox/cache.rb', line 264

def cache
  if INFINISPAN_AVAILABLE 
    @cache ||= manager.running?( name ) ? reconfigure : configure
  else
    @cache ||= nothing
  end
end

- (Object) clear



132
133
134
# File 'cache/lib/torquebox/cache.rb', line 132

def clear
  cache.clear
end

- (Boolean) clustered?

Returns:

  • (Boolean)


84
85
86
# File 'cache/lib/torquebox/cache.rb', line 84

def clustered?
  INFINISPAN_AVAILABLE && service.clustered? 
end

- (Object) clustering_mode



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'cache/lib/torquebox/cache.rb', line 88

def clustering_mode
  return CacheMode::LOCAL unless clustered?
  sync = options[:sync]
  case
  when replicated?
    sync ? CacheMode::REPL_SYNC : CacheMode::REPL_ASYNC
  when distributed?
    sync ? CacheMode::DIST_SYNC : CacheMode::DIST_ASYNC
  when invalidated?
    sync ? CacheMode::INVALIDATION_SYNC : CacheMode::INVALIDATION_ASYNC
  else
    sync ? CacheMode::DIST_SYNC : CacheMode::DIST_ASYNC
  end
end

- (Object) configuration(mode = clustering_mode)



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'cache/lib/torquebox/cache.rb', line 299

def configuration(mode=clustering_mode)
  config = ConfigurationBuilder.new.read( manager.default_cache_configuration )
  config.clustering.cacheMode( mode )
  config.transaction.transactionMode( transaction_mode )
  if transactional?
    config.transaction.transactionManagerLookup( transaction_manager_lookup )
    config.transaction.lockingMode( locking_mode )
  end
  if persisted?
    store = (config.respond_to?(:loaders) ? 
             config.loaders.add_file_cache_store :
             config.persistence.add_single_file_store)
    store.purgeOnStartup( false )
    store.location(options[:persist]) if File.exist?( options[:persist].to_s )
  end
  if ( options[:max_entries] ) 
    config.eviction.max_entries( options[:max_entries] )
    if ( options[:eviction] )
      config.eviction.strategy( eviction_strategy )
    end
  end
  config.build
end

- (Object) configure(mode = clustering_mode)



293
294
295
296
297
# File 'cache/lib/torquebox/cache.rb', line 293

def configure(mode=clustering_mode)
  log( "Configuring Infinispan cache #{name}" )
  manager.define_configuration(name, configuration(mode) )
  manager.get_cache(name)
end

- (Boolean) contains_key?(key)

Returns:

  • (Boolean)


150
151
152
# File 'cache/lib/torquebox/cache.rb', line 150

def contains_key?( key )
  cache.contains_key( encode(key) )
end

- (Object) decode(data)



256
257
258
# File 'cache/lib/torquebox/cache.rb', line 256

def decode(data)
  @codec.decode(data)
end

- (Object) decrement(name, amount = 1)



197
198
199
# File 'cache/lib/torquebox/cache.rb', line 197

def decrement(name, amount = 1)
  increment( name, -amount )
end

- (Boolean) distributed?

Returns:

  • (Boolean)


76
77
78
# File 'cache/lib/torquebox/cache.rb', line 76

def distributed?
  [:d, :dist, :distributed, :distribution].include? options[:mode]
end

- (Object) encode(data)



252
253
254
# File 'cache/lib/torquebox/cache.rb', line 252

def encode(data)
  @codec.encode(data)
end

- (Object) evict(key)



170
171
172
# File 'cache/lib/torquebox/cache.rb', line 170

def evict( key )
  cache.evict( encode(key) )
end

- (Object) eviction_strategy



118
119
120
121
122
123
124
# File 'cache/lib/torquebox/cache.rb', line 118

def eviction_strategy
  case options[:eviction]
  when :lirs then EvictionStrategy::LIRS
  when :lru then EvictionStrategy::LRU
  when :unordered then EvictionStrategy::UNORDERED
  end
end

- (Object) get(key)



155
156
157
# File 'cache/lib/torquebox/cache.rb', line 155

def get(key)
  decode(cache.get(encode(key)))
end

- (Object) increment(sequence_name, amount = 1)



183
184
185
186
187
188
189
190
191
192
193
194
# File 'cache/lib/torquebox/cache.rb', line 183

def increment(sequence_name, amount = 1)
  result, current = amount, get(sequence_name)
  if current.nil?
    put(sequence_name, result)
  else
    result = current + amount
    unless replace(sequence_name, current, result)
      raise "Concurrent modification, old value was #{current} new value #{result}"
    end
  end
  result
end

- (Boolean) invalidated?

Returns:

  • (Boolean)


80
81
82
# File 'cache/lib/torquebox/cache.rb', line 80

def invalidated?
  [:i, :inv, :invalidated, :invalidation].include? options[:mode] 
end

- (Object) keys



137
138
139
# File 'cache/lib/torquebox/cache.rb', line 137

def keys
  cache.key_set.map{|k| decode(k)}
end

- (Object) locking_mode



103
104
105
106
107
108
# File 'cache/lib/torquebox/cache.rb', line 103

def locking_mode
  case options[:locking_mode]
  when :optimistic then LockingMode::OPTIMISTIC
  when :pessimistic then LockingMode::PESSIMISTIC
  end
end

- (Object) log(message, status = 'INFO')



246
247
248
# File 'cache/lib/torquebox/cache.rb', line 246

def log( message, status = 'INFO' )
  TorqueBox::Infinispan::Cache.log( message, status )
end

- (Object) manager



276
277
278
# File 'cache/lib/torquebox/cache.rb', line 276

def manager
  @manager ||= service.cache_container
end

- (Object) max_entries



126
127
128
# File 'cache/lib/torquebox/cache.rb', line 126

def max_entries
  options[:max_entries] || -1
end

- (Object) name



64
65
66
# File 'cache/lib/torquebox/cache.rb', line 64

def name
  options[:name] || TORQUEBOX_APP_NAME
end

- (Object) nothing



361
362
363
364
365
366
# File 'cache/lib/torquebox/cache.rb', line 361

def nothing
  result = Object.new
  def result.method_missing(*args); end
  log( "Nothing: Can't get or create an Infinispan cache. No caching will occur", 'ERROR' ) if defined?(TORQUEBOX_APP_NAME)
  result
end

- (Object) options



260
261
262
# File 'cache/lib/torquebox/cache.rb', line 260

def options 
  @options ||= {}
end

- (Boolean) persisted?

Returns:

  • (Boolean)


68
69
70
# File 'cache/lib/torquebox/cache.rb', line 68

def persisted?
  !!options[:persist]
end

- (Object) put(key, value, expires = 0)



161
162
163
# File 'cache/lib/torquebox/cache.rb', line 161

def put(key, value, expires = 0)
  __put(key, value, expires, :put)
end

- (Object) put_if_absent(key, value, expires = 0)



166
167
168
# File 'cache/lib/torquebox/cache.rb', line 166

def put_if_absent(key, value, expires = 0)
  __put(key, value, expires, :put_if_absent)
end

- (Object) reconfigure(mode = clustering_mode)



280
281
282
283
284
285
286
287
288
289
290
291
# File 'cache/lib/torquebox/cache.rb', line 280

def reconfigure(mode=clustering_mode)
  existing_cache  = manager.get_cache(name)
  base_config = existing_cache.cache_configuration
  new_config = configuration(mode)
  unless same_config?(base_config, new_config)
    log("Reconfiguring Infinispan cache #{name}")
    existing_cache.stop
    manager.define_configuration(name, new_config )
    existing_cache.start
  end
  return existing_cache
end

- (Object) remove(key)



179
180
181
# File 'cache/lib/torquebox/cache.rb', line 179

def remove(key)
  decode(cache.remove(encode(key)))
end

- (Object) replace(key, original_value, new_value)



174
175
176
# File 'cache/lib/torquebox/cache.rb', line 174

def replace(key, original_value, new_value)
  cache.replace(encode(key), encode(original_value), encode(new_value))
end

- (Boolean) replicated?

Returns:

  • (Boolean)


72
73
74
# File 'cache/lib/torquebox/cache.rb', line 72

def replicated?
  [:r, :repl, :replicated, :replication].include? options[:mode]
end

- (Boolean) same_5_config?(c1, c2)

Returns:

  • (Boolean)


323
324
325
326
327
328
329
330
331
332
# File 'cache/lib/torquebox/cache.rb', line 323

def same_5_config?(c1, c2)
  c1.clustering.cacheMode == c2.clustering.cacheMode &&
    (c1.loaders == c2.loaders ||
     (c1.loaders.cacheLoaders.size == c2.loaders.cacheLoaders.size &&
      c1.loaders.cacheLoaders.first.location == c2.loaders.cacheLoaders.first.location)) &&
    c1.transaction.transactionMode == c2.transaction.transactionMode &&
    c1.transaction.lockingMode == c2.transaction.lockingMode &&
    c1.eviction.max_entries == c2.eviction.max_entries &&
    c1.eviction.strategy == c2.eviction.strategy
end

- (Boolean) same_6_config?(c1, c2)

Returns:

  • (Boolean)


334
335
336
337
338
339
340
341
342
343
# File 'cache/lib/torquebox/cache.rb', line 334

def same_6_config?(c1, c2)
  c1.clustering.cacheMode == c2.clustering.cacheMode &&
    (c1.persistence == c2.persistence ||
     (c1.persistence.stores.size == c2.persistence.stores.size &&
      c1.persistence.stores.first.location == c2.persistence.stores.first.location)) &&
    c1.transaction.transactionMode == c2.transaction.transactionMode &&
    c1.transaction.lockingMode == c2.transaction.lockingMode &&
    c1.eviction.max_entries == c2.eviction.max_entries &&
    c1.eviction.strategy == c2.eviction.strategy
end

- (Boolean) same_config?(c1, c2)

Returns:

  • (Boolean)


345
346
347
348
349
350
351
# File 'cache/lib/torquebox/cache.rb', line 345

def same_config?(c1, c2)
  if c1.respond_to?(:loaders)
    same_5_config?(c1, c2)
  else
    same_6_config?(c1, c2)
  end
end

- (Object) service



272
273
274
# File 'cache/lib/torquebox/cache.rb', line 272

def service
  @service ||= TorqueBox::ServiceRegistry[CacheService::CACHE]
end

- (Object) size



141
142
143
# File 'cache/lib/torquebox/cache.rb', line 141

def size
  cache.size
end

- (Object) stop



238
239
240
# File 'cache/lib/torquebox/cache.rb', line 238

def stop
  cache.stop
end

- (Object) transaction(&block)



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'cache/lib/torquebox/cache.rb', line 201

def transaction(&block)
  if !transactional?
    yield self
  elsif TorqueBox.fetch('transaction-manager').nil?
    tm = cache.getAdvancedCache.getTransactionManager
    begin
      tm.begin if tm
      yield self
      tm.commit if tm
    rescue Exception => e
      if tm.nil?
        log( "Transaction is nil", "ERROR" )
        log( e.message, 'ERROR' )
        log( e.backtrace, 'ERROR' )
      elsif tm.status == javax.transaction.Status.STATUS_NO_TRANSACTION
        log( "No transaction was started", "ERROR" )
        log( e.message, 'ERROR' )
        log( e.backtrace, 'ERROR' )
      else
        tm.rollback 
        log( "Rolling back.", 'ERROR' )
        log( e.message, 'ERROR' )
        log( e.backtrace, 'ERROR' )
      end
      raise e
    end
  else
    TorqueBox.transaction do 
      yield self 
    end
  end
end

- (Object) transaction_manager_lookup



353
354
355
356
357
358
359
# File 'cache/lib/torquebox/cache.rb', line 353

def transaction_manager_lookup
  @tm ||= if TorqueBox.fetch('transaction-manager')
            ContainerTransactionManagerLookup.new(TorqueBox.fetch('transaction-manager'))
          else
            org.infinispan.transaction.lookup.GenericTransactionManagerLookup.new
          end
end

- (Object) transaction_mode



110
111
112
# File 'cache/lib/torquebox/cache.rb', line 110

def transaction_mode
  options[:transaction_mode] == :transactional ? TransactionMode::TRANSACTIONAL : TransactionMode::NON_TRANSACTIONAL
end

- (Boolean) transactional?

Returns:

  • (Boolean)


114
115
116
# File 'cache/lib/torquebox/cache.rb', line 114

def transactional?
  transaction_mode == TransactionMode::TRANSACTIONAL
end