forked from mybatis/mybatis-3
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for mybatis#116. Default implementation for a blocking cache and …
…support for EhCache's impl provided by Iwao
- Loading branch information
Showing
14 changed files
with
451 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,6 @@ | |
|
||
boolean readWrite() default true; | ||
|
||
boolean blocking() default false; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/main/java/org/apache/ibatis/cache/decorators/BlockingCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package org.apache.ibatis.cache.decorators; | ||
/* | ||
* Copyright 2009-2014 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReadWriteLock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import org.apache.ibatis.cache.Cache; | ||
import org.apache.ibatis.cache.CacheException; | ||
|
||
/** | ||
* Simple blocking decorator | ||
* | ||
* Sipmle and inefficient version of EhCache's BlockingCache decorator. | ||
* It sets a lock over a cache key when the element is not found in cache. | ||
* This way, other threads will wait until this element is filled instead of hitting the database. | ||
* | ||
* @author Eduardo Macarron | ||
* | ||
*/ | ||
public class BlockingCache implements Cache { | ||
|
||
private long timeout; | ||
private final Cache delegate; | ||
private final ConcurrentHashMap<Object, ReentrantLock> locks; | ||
|
||
public BlockingCache(Cache delegate) { | ||
this.delegate = delegate; | ||
this.locks = new ConcurrentHashMap<Object, ReentrantLock>(); | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return delegate.getId(); | ||
} | ||
|
||
@Override | ||
public int getSize() { | ||
return delegate.getSize(); | ||
} | ||
|
||
@Override | ||
public void putObject(Object key, Object value) { | ||
try { | ||
delegate.putObject(key, value); | ||
} finally { | ||
releaseLock(key); | ||
} | ||
} | ||
|
||
@Override | ||
public Object getObject(Object key) { | ||
acquireLock(key); | ||
Object value = delegate.getObject(key); | ||
if (value != null) { | ||
releaseLock(key); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public Object removeObject(Object key) { | ||
return delegate.removeObject(key); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
delegate.clear(); | ||
} | ||
|
||
@Override | ||
public ReadWriteLock getReadWriteLock() { | ||
return null; | ||
} | ||
|
||
private ReentrantLock getLockForKey(Object key) { | ||
ReentrantLock lock = new ReentrantLock(); | ||
ReentrantLock previous = locks.putIfAbsent(key, lock); | ||
return previous == null ? lock : previous; | ||
} | ||
|
||
private void acquireLock(Object key) { | ||
Lock lock = getLockForKey(key); | ||
if (timeout > 0) { | ||
try { | ||
boolean acquired = lock.tryLock(timeout, TimeUnit.MILLISECONDS); | ||
if (!acquired) { | ||
throw new CacheException("Couldn't get a lock in " + timeout + " for the key " + key + " at the cache " + delegate.getId()); | ||
} | ||
} catch (InterruptedException e) { | ||
throw new CacheException("Got interrupted while trying to acquire lock for key " + key, e); | ||
} | ||
} else { | ||
lock.lock(); | ||
} | ||
} | ||
|
||
private void releaseLock(Object key) { | ||
ReentrantLock lock = locks.get(key); | ||
if (lock.isHeldByCurrentThread()) { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public long getTimeout() { | ||
return timeout; | ||
} | ||
|
||
public void setTimeout(long timeout) { | ||
this.timeout = timeout; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.