Using the MemoryService
Information
This explains basic usage of the Sakai MemoryService. This service manages and is used to create and retrieve information about existing caches.
Trunk javadocs:
Trunk source location: https://source.sakaiproject.org/svn/kernel/trunk/api/src/main/java/org/sakaiproject/memory/
Accessing the MemoryService
You can use Spring Framework to inject the service or use the cover
Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
Add the MemoryService bean to the bean for YourAppClass
<bean id="org.sakaiproject.yourapp.logic.YourAppClass" class="org.sakaiproject.yourapp.logic.impl.YourAppClassImpl"> <property name="memoryService" ref="org.sakaiproject.memory.api.MemoryService" /> </bean>Add a variable and setter to YourAppClass to use the service in like so:
private MemoryService memoryService; public void setMemoryService(MemoryService memoryService) { this.memoryService = memoryService; }
Using the Component Manager to get the service
Note: This is not the recommended method, you should be using Spring to inject the service
Use the CM cover to get the service
import org.sakaiproject.component.cover.ComponentManager; ... private MemoryService memoryService; ... memoryService = (MemoryService) ComponentManager.get(MemoryService.class);
Getting a Cache by name
Use the service variable to access the service and request a Cache. This will create one if needed or retrieve an existing one if it was created earlier.
Cache myCache = memoryService.getCache("org.sakaiproject.my.MyService.myCache");Note: This returns a Sakai Cache object