4 Replies Latest reply on May 12, 2016 4:04 AM by Tomas Jurak

    Local cache persistence to file-store

    Tomas Jurak Newbie

      Dear Infinispan community,

      I have probably a basic question. I have set up Infinispan 8.2.1.Final with Spring and setup a local cache in infinispan.xml file:

       

       

      <?xml version="1.0" encoding="UTF-8"?>

      <infinispan

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xsi:schemaLocation="urn:infinispan:config:8.2 http://www.infinispan.org/schemas/infinispan-config-8.2.xsd"

            xmlns="urn:infinispan:config:8.2">

       

        <cache-container default-cache="unitCache">

        <local-cache name="unitCache">

        <indexing index="NONE" />

        <transaction mode="NONE" />

        <eviction strategy="NONE" size="32" />

        <persistence passivation="false">

        <file-store path="unitCache" preload="true" purge="false" read-only="false" singleton="false" shared="false"/>

        </persistence>

        </local-cache>

        </cache-container>

      </infinispan>

       

      I can load objects into this cache and read them well in the Java code (also by different threads). I can set and get object via cache.get() and cache.put() methods and then change somethig on this object.

      ...

      obj.setAttribute(100);

      cache.put(obj); // OK, this works fine the object is stored in memory as well as in file storage

      ...

      MyObject obj = cache.get("key");

      obj.getAttribute(); // This will return 100, OK

      obj.setAttribute(10); // This will be stored only in memory and will not persist into the file

       

      The qustion is: Why method setAttribute(10) on object get from cache will change the value of object in memory but not in the persistent file? Do I really need to call cache.put() to persist new data to file?

      (I'm pretty sure that in Infinispan 6.0.2.Final calling obj.setAttribute(10); would persist change into the file too)

       

      Thank you for any help or enlightening.

       

      Regards,

      Tomas

        • 1. Re: Local cache persistence to file-store
          Radim Vansa Master

          Infinispan does not intercept any method calls on your objects to 'flush' them into cache store, neither in version 6.0.2 or any other.

           

          Storing the object itself (as opposite to storing a copy of it) in local cache is more of a performance optimization (because local caches are meant to be fast). Clustered caches always store copy of the object.

           

          You're not supposed to modify the object after storing in the cache (without explicitly overwriting it), and ideally the object should be immutable to avoid threading issues. So your scenario is not supported by design.

          1 of 1 people found this helpful
          • 2. Re: Local cache persistence to file-store
            Tomas Jurak Newbie

            Thank you for answer!

            So if I understand it right: if I recreate the cache to be remote, it will store copy of objects which I'm directly manipulating in Java code and Infinispan will store them to file storage everytime I make a change in the object? Or Is there any API in Infinispan which could help me to achieve behavior of having "all-time" synchronized objects between memory and file storage? I hope that I will be able to write some solution for this.

             

            Tomas

            • 3. Re: Local cache persistence to file-store
              Radim Vansa Master

              Sorry, the first question is not very clear:

              if I recreate the cache to be remote, it will store copy of objects which I'm directly manipulating in Java code

              Yes, after calling cache.put(key, value), Infinispan will make a defensive copy of value, and store that copy. When you do a cache.get(key), you'll get a copy of the internally stored copy.

              Infinispan will store them to file storage everytime I make a change in the object

              No, Infinispan won't check any changes to your object you do, unless you explicitly call cache.put() - and then it will again create the defensive copy of the modified object.

              Or Is there any API in Infinispan which could help me to achieve behavior of having "all-time" synchronized objects between memory and file storage?

              Not with pure Infinispan. You might consider Hibernate OGM, with that you can automatically get proxy for that object http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#merge%28T%29, and all modifications will be persisted on transaction commit (maybe all the time with autocommit). But it might be an overkill for your usecase.

              1 of 1 people found this helpful
              • 4. Re: Local cache persistence to file-store
                Tomas Jurak Newbie

                Thank you for help, I understand it now. I will definitely look on Hibernate OGM - maybe this is the way to go with our use case.