Class: TorqueBox::Infinispan::Cache

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cache

Returns a new instance of Cache.



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

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

.log(message, status = 'INFO') ⇒ Object



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

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

Instance Method Details

#add_listener(listener) ⇒ Object



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

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

#allObject Also known as: values



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

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

#clearObject

Clear the entire cache. Be careful with this method since it could affect other processes if shared cache is being used.



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

def clear
  cache.clear
end

#clustered?Boolean

Returns:

  • (Boolean)


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

def clustered?
  INFINISPAN_AVAILABLE && service.clustered? 
end

#clustering_modeObject



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

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

#contains_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#decrement(name, amount = 1) ⇒ Object

Decrement an integer value in the cache; return new value



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

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

#distributed?Boolean

Returns:

  • (Boolean)


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

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

#evict(key) ⇒ Object



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

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

#eviction_strategyObject



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

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

#get(key) ⇒ Object Also known as: []

Get an entry from the cache



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

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

#increment(sequence_name, amount = 1) ⇒ Object



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

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

#invalidated?Boolean

Returns:

  • (Boolean)


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

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

#keysObject

Return the keys in the cache; potentially very expensive depending on configuration



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

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

#locking_modeObject



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

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

#log(message, status = 'INFO') ⇒ Object



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

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

#max_entriesObject



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

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

#nameObject



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

def name
  options[:name] || TORQUEBOX_APP_NAME
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !!options[:persist]
end

#put(key, value, expires = 0) ⇒ Object Also known as: []=

Write an entry to the cache



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

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

#put_if_absent(key, value, expires = 0) ⇒ Object



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

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

#remove(key) ⇒ Object

Delete an entry from the cache



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

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

#replace(key, original_value, new_value) ⇒ Object



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

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

#replicated?Boolean

Returns:

  • (Boolean)


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

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

#sizeObject



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

def size
  cache.size
end

#stopObject



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

def stop
  cache.stop
end

#transaction(&block) ⇒ Object



200
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
# File 'lib/torquebox/cache.rb', line 200

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

#transaction_modeObject



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

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

#transactional?Boolean

Returns:

  • (Boolean)


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

def transactional?
  transaction_mode == TransactionMode::TRANSACTIONAL
end