Java: HashMap object cloning issue

Status
Not open for further replies.

TheDan666

Ars Tribunus Militum
2,359
I have an issue that I've been banging my head at for a while. I've been tasked with a building a Java cache toolkit with various implementations all belonging to a single cache interface. One of the implementations is an actual HashMap. The relevant parts of the code are:

Code:
private HashMap cache = new HashMap();

 public Object get (Object key) throws CacheException
  {
    try
    {
      return cache.get(key)
    }
    catch (Exception e)
    {
      throw new CacheException("Failed to get key:" + key + " from cache", e);
    }
  }


 public void put (Object key, Object value) throws CacheException
  {
    synchronized(this.cache) {
      try
      {
        this.cache.put(key, value);
        logger.info("putting key:" + key + " value: " + value + " into cache");
      }
      catch (Exception e)
      {
        throw new CacheException("Failed to put key:" + key + " and value:" + value + "  into cache", e);
      }
    }

  }

The issue I'm having is that when a user gets an object from the hashamp cache and assigns it a local variable and then changes something in that object via a setter, the actual cache is being affected.

I would like to return a cloned value of the value derived from get(key) but since the interface is purely object I cannot clone it. Any ideas?

Thanks,
The Dan
 

TheDan666

Ars Tribunus Militum
2,359
andrewme":2s826b18 said:
That still doesn't necessarily "work" - the object you clone may have references to other objects, which would be handled properly by a real clone method.

Are you saying the serializing/deserializing won't work or a standard clone won't work? Regarding a "real" clone method, I don't see how I can even have one since I have no idea what the object will even be so I can't call a clone() method on it. And even if I could inspect the object via reflection and somehow call a clone method, it seems like a bad thing because any object that anyone would ever want to cache would have to implement cloneable which would be a PITA and basically unworkable.

--The Dan
 

TheDan666

Ars Tribunus Militum
2,359
DrPizza":117udep6 said:
The issue I'm having is that when a user gets an object from the hashamp cache and assigns it a local variable and then changes something in that object via a setter, the actual cache is being affected.
As it should be.

Well, that was kind of my reaction as well. My feeling is that a cache is for reference (pardon the ironic pun). If you want to change the values then put in something new to overwrite what's in there already.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
The only thing capable of cloning an arbitrary object successfully _and being sure it did_ is the object itself (perhaps recursively).
Since there is no standard DeepClonable interface (unless one got added to java without me knowing) you have no standard way of hooking into such a process.

If you are storing mutable objects in a cache they had better have at least immutable equality/hashcode backing fields (the default object.equals(), and hashCode() are sufficient). Objects placed into a cache should be designed such that this is acceptable. Thinking you can throw anything into it is a bad idea.

Also I think you have a threading issue since you do not synchronize your map lookups. It may be possible to corrupt your state as a result.

Also you didn't post full code but this being a cache I would direct you at Cache with Policy.
 

TheDan666

Ars Tribunus Militum
2,359
cmannes":8njkvb9p said:
I'm sorry for the lack of help in this post. :)

But is there a reason why you can't use an existing Java Caching implementation?

JCS Maybe? - http://jakarta.apache.org/jcs/

Well the code will be implementing several existing Java cache systems: Gigaspaces and Oracle Coherence among them. But one of the required implementations is standard hashmap.

--The Dan
 
Status
Not open for further replies.