Hello,
If you want to use the liferay's Cache mechanism for storing redundant network calls for improving the performance of the portal, Liferay has given api to store those values in the cache, below is thesample program .
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
package com.sample.service.service.impl; | |
import com.liferay.portal.kernel.cache.MultiVMPoolUtil; | |
import com.liferay.portal.kernel.cache.PortalCache; | |
import com.liferay.portal.kernel.log.Log; | |
import com.liferay.portal.kernel.log.LogFactoryUtil; | |
import com.liferay.portal.kernel.util.Validator; | |
import com.sample.service.service.base.StudentServiceBaseImpl; | |
import java.io.Serializable; | |
public class StudentServiceImpl extends StudentServiceBaseImpl { | |
private static final Log _log = LogFactoryUtil.getLog(StudentServiceImpl.class); | |
public void testCache(String key,String value){ | |
/** The below statement will return the cache with the name test | |
If the cache does not exists with the name cache it will create and return it. */ | |
PortalCache<Serializable, Serializable> cache = MultiVMPoolUtil.getCache("test"); | |
//Get the value from cache like below | |
Serializable serializable = cache.get(key); | |
/** Here i am checking if the passed key exists or not. | |
If does not exists I am putting it in the cache, | |
if it already exists i am getting the value from cache and returning it. */ | |
if(Validator.isNull(serializable)){ | |
cache.put(key, value); | |
_log.info("added into cache"); | |
}else{ | |
_log.info("found in the cache: "+cache.get(key)); | |
} | |
} | |
} |
Comments