Resul Rzaeeff
Resul Rzaeeff

Reputation: 488

Spring Ehcache refresh

I read my data from cache . I am using ehcache. I need to refresh my cache data after every insert action. How i can do it with annotation ? or any other way ?

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">

 <diskStore path="java.io.tmpdir" />

 <defaultCache
    maxElementsInMemory="10000" 
    eternal="true"
    timeToIdleSeconds="120" 
    timeToLiveSeconds="120" 
    overflowToDisk="true"
    maxElementsOnDisk="10000000" 
    diskPersistent="true"
    diskExpiryThreadIntervalSeconds="120" 
    memoryStoreEvictionPolicy="LRU" />

<cache name="allNodesCache"
    maxEntriesLocalHeap="10000"
    maxEntriesLocalDisk="1000"
    eternal="false"
    diskSpoolBufferSizeMB="20"
    timeToIdleSeconds="300" timeToLiveSeconds="600"
    memoryStoreEvictionPolicy="LFU"
    transactionalMode="off">
    <persistence strategy="localTempSwap" />
</cache>

<cache name="nodesMAPCache"
    maxElementsInMemory="10000" 
    eternal="true"
    timeToIdleSeconds="120" 
    timeToLiveSeconds="120" 
    overflowToDisk="true"
    maxElementsOnDisk="10000000" 
    diskPersistent="true"
    diskExpiryThreadIntervalSeconds="120" 
    memoryStoreEvictionPolicy="LRU" />

<cache name="nodesCache"
    maxElementsInMemory="10000" 
    eternal="true"
    timeToIdleSeconds="120" 
    timeToLiveSeconds="120" 
    overflowToDisk="true"
    maxElementsOnDisk="10000000" 
    diskPersistent="true"
    diskExpiryThreadIntervalSeconds="120" 
    memoryStoreEvictionPolicy="LRU" />

Repository class:

Repository

public interface NodeRepository extends JpaRepository {

@Cacheable(value = "nodesCache")
@Query("select new com.datum.fnd.domain.Node(c.idNode, c.name, c.address, c.description, c.point) from Node c")
List<Node> selectAll();

Upvotes: 0

Views: 1821

Answers (1)

Resul Rzaeeff
Resul Rzaeeff

Reputation: 488

I have solved my problem with following code:

@CacheEvict(cacheNames = { "nodesCache", "nodesMAPCache" }, allEntries = true)
@Override
public Node create(Node node) {
    node.setName(nodeRepository.generateNodeName(node.getRegion().getIdRegion()));
    return nodeRepository.save(node);
}

Upvotes: 1

Related Questions