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.

Accessing the MemoryService

  • You can use Spring Framework to inject the service or use the cover
  1. Using Spring to get the service for your class (e.g. YourAppClass) (recommended)
    1. 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>
      
    2. 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;
      }
      
  2. Using the Component Manager to get the service
    • Note: This is not the recommended method, you should be using Spring to inject the service
    1. 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

  1. 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