Class: TorqueBox::Infinispan::Cache
- Inherits:
-
Object
- Object
- TorqueBox::Infinispan::Cache
- Defined in:
- lib/torquebox/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 (also: #values)
-
#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
- #eviction_strategy ⇒ Object
-
#get(key) ⇒ Object
(also: #[])
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
- #max_entries ⇒ Object
- #name ⇒ Object
- #persisted? ⇒ Boolean
-
#put(key, value, expires = 0) ⇒ Object
(also: #[]=)
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) ⇒ Object
- #replicated? ⇒ Boolean
- #size ⇒ Object
- #stop ⇒ Object
- #transaction(&block) ⇒ Object
- #transaction_mode ⇒ Object
- #transactional? ⇒ Boolean
Constructor Details
#initialize(opts = {}) ⇒ Cache
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 'lib/torquebox/cache.rb', line 45 def initialize(opts = {}) @options = opts if INFINISPAN_AVAILABLE [:transaction_mode] = :transactional unless .has_key?( :transaction_mode ) [:locking_mode] ||= :optimistic if (transactional? && !.has_key?( :locking_mode )) [:sync] = true if [:sync].nil? end if [:encoding] == :marshal log( "Encoding of :marshal cannot be used with " + "TorqueBox::Infinispan::Cache - using :marshal_base64 instead", 'WARN') [:encoding] = :marshal_base64 end @codec = TorqueBox::Codecs[ [:encoding] || :marshal_smart ] cache end |
Class Method Details
.log(message, status = 'INFO') ⇒ Object
242 243 244 |
# File 'lib/torquebox/cache.rb', line 242 def self.log( , status = 'INFO' ) $stdout.puts( "#{status}: #{}" ) end |
Instance Method Details
#add_listener(listener) ⇒ Object
234 235 236 |
# File 'lib/torquebox/cache.rb', line 234 def add_listener( listener ) cache.add_listener( listener ) end |
#all ⇒ Object Also known as: values
145 146 147 |
# File 'lib/torquebox/cache.rb', line 145 def all keys.map{|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.
132 133 134 |
# File 'lib/torquebox/cache.rb', line 132 def clear cache.clear end |
#clustered? ⇒ Boolean
84 85 86 |
# File 'lib/torquebox/cache.rb', line 84 def clustered? INFINISPAN_AVAILABLE && service.clustered? end |
#clustering_mode ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/torquebox/cache.rb', line 88 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
150 151 152 |
# File 'lib/torquebox/cache.rb', line 150 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
197 198 199 |
# File 'lib/torquebox/cache.rb', line 197 def decrement(name, amount = 1) increment( name, -amount ) end |
#distributed? ⇒ Boolean
76 77 78 |
# File 'lib/torquebox/cache.rb', line 76 def distributed? [:d, :dist, :distributed, :distribution].include? [:mode] end |
#evict(key) ⇒ Object
170 171 172 |
# File 'lib/torquebox/cache.rb', line 170 def evict( key ) cache.evict( encode(key) ) end |
#eviction_strategy ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/torquebox/cache.rb', line 118 def eviction_strategy case [: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
155 156 157 |
# File 'lib/torquebox/cache.rb', line 155 def get(key) decode(cache.get(encode(key))) end |
#increment(sequence_name, amount = 1) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 |
# File '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 |
#invalidated? ⇒ Boolean
80 81 82 |
# File 'lib/torquebox/cache.rb', line 80 def invalidated? [:i, :inv, :invalidated, :invalidation].include? [:mode] end |
#keys ⇒ Object
Return the keys in the cache; potentially very expensive depending on configuration
137 138 139 |
# File 'lib/torquebox/cache.rb', line 137 def keys cache.key_set.map{|k| decode(k)} end |
#locking_mode ⇒ Object
103 104 105 106 107 108 |
# File 'lib/torquebox/cache.rb', line 103 def locking_mode case [:locking_mode] when :optimistic then LockingMode::OPTIMISTIC when :pessimistic then LockingMode::PESSIMISTIC end end |
#log(message, status = 'INFO') ⇒ Object
246 247 248 |
# File 'lib/torquebox/cache.rb', line 246 def log( , status = 'INFO' ) TorqueBox::Infinispan::Cache.log( , status ) end |
#max_entries ⇒ Object
126 127 128 |
# File 'lib/torquebox/cache.rb', line 126 def max_entries [:max_entries] || -1 end |
#name ⇒ Object
64 65 66 |
# File 'lib/torquebox/cache.rb', line 64 def name [:name] || TORQUEBOX_APP_NAME end |
#persisted? ⇒ Boolean
68 69 70 |
# File 'lib/torquebox/cache.rb', line 68 def persisted? !![:persist] end |
#put(key, value, expires = 0) ⇒ Object Also known as: []=
Write an entry to the cache
161 162 163 |
# File 'lib/torquebox/cache.rb', line 161 def put(key, value, expires = 0) __put(key, value, expires, :put) end |
#put_if_absent(key, value, expires = 0) ⇒ Object
166 167 168 |
# File 'lib/torquebox/cache.rb', line 166 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
179 180 181 |
# File 'lib/torquebox/cache.rb', line 179 def remove(key) decode(cache.remove(encode(key))) end |
#replace(key, original_value, new_value) ⇒ Object
174 175 176 |
# File 'lib/torquebox/cache.rb', line 174 def replace(key, original_value, new_value) cache.replace(encode(key), encode(original_value), encode(new_value)) end |
#replicated? ⇒ Boolean
72 73 74 |
# File 'lib/torquebox/cache.rb', line 72 def replicated? [:r, :repl, :replicated, :replication].include? [:mode] end |
#size ⇒ Object
141 142 143 |
# File 'lib/torquebox/cache.rb', line 141 def size cache.size end |
#stop ⇒ Object
238 239 240 |
# File 'lib/torquebox/cache.rb', line 238 def stop cache.stop end |
#transaction(&block) ⇒ Object
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 '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., '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
110 111 112 |
# File 'lib/torquebox/cache.rb', line 110 def transaction_mode [:transaction_mode] == :transactional ? TransactionMode::TRANSACTIONAL : TransactionMode::NON_TRANSACTIONAL end |
#transactional? ⇒ Boolean
114 115 116 |
# File 'lib/torquebox/cache.rb', line 114 def transactional? transaction_mode == TransactionMode::TRANSACTIONAL end |