Class: TorqueBox::Infinispan::Cache
- Inherits:
-
Object
- Object
- TorqueBox::Infinispan::Cache
- Includes:
- TorqueBox::Injectors
- Defined in:
- lib/cache.rb
Constant Summary collapse
- SECONDS =
java.util.concurrent.TimeUnit::SECONDS
Class Method Summary collapse
Instance Method Summary collapse
- #add_listener(listener) ⇒ Object
- #all ⇒ Object
-
#clear ⇒ Object
Clear the entire cache.
- #clustered? ⇒ Boolean
- #clustering_mode ⇒ Object
- #contains_key?(key) ⇒ Boolean
-
#decrement(name, amount = 1) ⇒ Object
Decrement an integer value in the cache; return new value.
- #distributed? ⇒ Boolean
- #evict(key) ⇒ Object
-
#get(key) ⇒ Object
Get an entry from the cache.
- #increment(sequence_name, amount = 1) ⇒ Object
-
#initialize(opts = {}) ⇒ Cache
constructor
A new instance of Cache.
- #invalidated? ⇒ Boolean
-
#keys ⇒ Object
Return the keys in the cache; potentially very expensive depending on configuration.
- #locking_mode ⇒ Object
- #log(message, status = 'INFO') ⇒ Object
- #name ⇒ Object
- #persisted? ⇒ Boolean
-
#put(key, value, expires = 0) ⇒ Object
Write an entry to the cache.
- #put_if_absent(key, value, expires = 0) ⇒ Object
-
#remove(key) ⇒ Object
Delete an entry from the cache.
- #replace(key, original_value, new_value, codec = NoOpCodec) ⇒ Object
- #replicated? ⇒ Boolean
- #stop ⇒ Object
- #transaction(&block) ⇒ Object
- #transaction_mode ⇒ Object
- #transactional? ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Cache
Returns a new instance of Cache.
66 67 68 69 70 71 72 73 |
# File 'lib/cache.rb', line 66 def initialize(opts = {}) return nothing unless INFINISPAN_AVAILABLE @options = opts [:transaction_mode] = :transactional unless .has_key?( :transaction_mode ) [:locking_mode] ||= :optimistic if (transactional? && !.has_key?( :locking_mode )) [:sync] = true if [:sync].nil? cache end |
Class Method Details
.log(message, status = 'INFO') ⇒ Object
248 249 250 |
# File 'lib/cache.rb', line 248 def self.log( , status = 'INFO' ) $stdout.puts( "#{status}: #{}" ) end |
Instance Method Details
#add_listener(listener) ⇒ Object
240 241 242 |
# File 'lib/cache.rb', line 240 def add_listener( listener ) cache.add_listener( listener ) end |
#all ⇒ Object
140 141 142 |
# File 'lib/cache.rb', line 140 def all cache.key_set.collect{|k| get(k)} end |
#clear ⇒ Object
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/cache.rb', line 131 def clear cache.clearAsync end |
#clustered? ⇒ Boolean
95 96 97 |
# File 'lib/cache.rb', line 95 def clustered? INFINISPAN_AVAILABLE && service.clustered? end |
#clustering_mode ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cache.rb', line 99 def clustering_mode return CacheMode::LOCAL unless clustered? sync = [: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
144 145 146 |
# File 'lib/cache.rb', line 144 def contains_key?( key ) cache.contains_key( key.to_s ) end |
#decrement(name, amount = 1) ⇒ Object
Decrement an integer value in the cache; return new value
203 204 205 |
# File 'lib/cache.rb', line 203 def decrement(name, amount = 1) increment( name, -amount ) end |
#distributed? ⇒ Boolean
87 88 89 |
# File 'lib/cache.rb', line 87 def distributed? [:d, :dist, :distributed, :distribution].include? [:mode] end |
#evict(key) ⇒ Object
162 163 164 |
# File 'lib/cache.rb', line 162 def evict( key ) cache.evict( key.to_s ) end |
#get(key) ⇒ Object
Get an entry from the cache
149 150 151 |
# File 'lib/cache.rb', line 149 def get(key) cache.get( key.to_s ) end |
#increment(sequence_name, amount = 1) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/cache.rb', line 185 def increment( sequence_name, amount = 1 ) current_entry = Sequence::Codec.decode( get( sequence_name ) ) # If we can't find the sequence in the cache, create a new one and return put( sequence_name, Sequence::Codec.encode( Sequence.new( amount ) ) ) and return amount if current_entry.nil? # Increment the sequence, stash it, and return next_entry = current_entry.next( amount ) # Since replace() doesn't encode, let's encode everything to a byte[] for it, no? if replace( sequence_name, current_entry, next_entry, Sequence::Codec ) return next_entry.value else raise "Concurrent modification, old value was #{current_entry.value} new value #{next_entry.value}" end end |
#invalidated? ⇒ Boolean
91 92 93 |
# File 'lib/cache.rb', line 91 def invalidated? [:i, :inv, :invalidated, :invalidation].include? [:mode] end |
#keys ⇒ Object
Return the keys in the cache; potentially very expensive depending on configuration
136 137 138 |
# File 'lib/cache.rb', line 136 def keys cache.key_set end |
#locking_mode ⇒ Object
114 115 116 117 118 119 |
# File 'lib/cache.rb', line 114 def locking_mode case [:locking_mode] when :optimistic then LockingMode::OPTIMISTIC when :pessimistic then LockingMode::PESSIMISTIC end end |
#log(message, status = 'INFO') ⇒ Object
252 253 254 |
# File 'lib/cache.rb', line 252 def log( , status = 'INFO' ) TorqueBox::Infinispan::Cache.log( , status ) end |
#name ⇒ Object
75 76 77 |
# File 'lib/cache.rb', line 75 def name [:name] || TORQUEBOX_APP_NAME end |
#persisted? ⇒ Boolean
79 80 81 |
# File 'lib/cache.rb', line 79 def persisted? ![:persist].nil? end |
#put(key, value, expires = 0) ⇒ Object
Write an entry to the cache
154 155 156 |
# File 'lib/cache.rb', line 154 def put(key, value, expires = 0) __put(key, value, expires, :put_async) end |
#put_if_absent(key, value, expires = 0) ⇒ Object
158 159 160 |
# File 'lib/cache.rb', line 158 def put_if_absent(key, value, expires = 0) __put(key, value, expires, :put_if_absent_async) end |
#remove(key) ⇒ Object
Delete an entry from the cache
181 182 183 |
# File 'lib/cache.rb', line 181 def remove(key) cache.removeAsync( key.to_s ) && true end |
#replace(key, original_value, new_value, codec = NoOpCodec) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/cache.rb', line 166 def replace(key, original_value, new_value, codec=NoOpCodec) # First, grab the raw value from the cache, which is a byte[] current = get( key ) decoded = codec.decode( current ) # great! we've got a byte[] now. Let's apply == to it, like Jim says will work always if ( decoded == original_value ) # how does this work? cache.replace( key.to_s, current, codec.encode( new_value ) ) end end |
#replicated? ⇒ Boolean
83 84 85 |
# File 'lib/cache.rb', line 83 def replicated? [:r, :repl, :replicated, :replication].include? [:mode] end |
#stop ⇒ Object
244 245 246 |
# File 'lib/cache.rb', line 244 def stop cache.stop end |
#transaction(&block) ⇒ Object
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 233 234 235 236 237 238 |
# File 'lib/cache.rb', line 207 def transaction(&block) if !transactional? yield self elsif 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., 'ERROR' ) log( e.backtrace, 'ERROR' ) elsif tm.status == javax.transaction.Status.STATUS_NO_TRANSACTION log( "No transaction was started", "ERROR" ) log( e., 'ERROR' ) log( e.backtrace, 'ERROR' ) else tm.rollback log( "Rolling back.", 'ERROR' ) log( e., 'ERROR' ) log( e.backtrace, 'ERROR' ) end raise e end else TorqueBox.transaction do yield self end end end |
#transaction_mode ⇒ Object
121 122 123 |
# File 'lib/cache.rb', line 121 def transaction_mode [:transaction_mode] == :transactional ? TransactionMode::TRANSACTIONAL : TransactionMode::NON_TRANSACTIONAL end |
#transactional? ⇒ Boolean
125 126 127 |
# File 'lib/cache.rb', line 125 def transactional? transaction_mode == TransactionMode::TRANSACTIONAL end |