I like this approach, but it leads me to another question.
The CacheManager object expects a Cache<Object,Object> type. For example, if I
compile my Cache<String,Object> and CacheManager 'getCache' method, the compile
warns that the return object is the incorrect type because the CacheManager
wants to return Cache<Object,Object>.
@Override
public Cache<String, Object> getCache(String name) throws CacheException {
try {
Memcache mc = null;
if (cacheTable.containsKey(name)) {
log.info("Found existing memcached client.");
mc = cacheTable.get(name);
}
else {
log.info("Constructing new memcached client and storing is for reuse.");
mc = new Memcache( new MemcachedClient( new InetSocketAddress("192.168.0.242",
11211)));
cacheTable.put(name, mc);
}
return mc;
}
catch(IOException ex) {
log.error(ex);
}
return null;
}
So I'm unlclear on whether if I implement my cache as type
Cache<String,Object>, if that will pose a problem for shiro. For example,
will shiro ever use a non String key to access the cache?
---James
----- Original Message -----
From: [email protected]
To: [email protected]
Sent: Tuesday, July 19, 2011 10:33 PM
Subject: Re: Question about implenting Cache<K,V>
You probably don't want to do that because you lose the static typing that
generics are meant to provide. A more appropriate implementation would be
something like:
public class ExampleCache implements Cache<String, Object> {
@Override
public Object get( String key ) throws CacheException {
return mc.get( key );
}
// Other methods
}
If your cache contains only one type of object that is more specific than
Object, you could potentially use that type instead and perform a (unsafe) cast
within the definition of get(String).
On Wed, Jul 20, 2011 at 1:22 AM, James Whetstone - [email protected]
wrote:
This question might be more about proper Java than it is about the
Cache<K,V> interface, but I wouldn't know where else to ask this.
I'm implementing a Cache<K,V> interface for memached and I'm using
spymemcached. The spymemcached client only takes a string as the key.
So I've implemented, for example, the get(K key) method it by casting the
arguments as shown below.
@Override
public
V get(K key) throws CacheException {
return (V) mc.get((String) key);
}
So the question is, is this safe/proper use of Java in this context? It
doesn't look right to me.
Thanks,
James