Friday, January 20, 2017

How to put objects in memcache?

How to put objects in memcache

This example shows how to put objects in the memcached server.

Use the set(...) method of net.spy.memcached.MemcachedClient object, passing it the unique name by which to identify the object, expiry time period of the object in seconds and the object itself. In the example below, the expiry time used is 3600 seconds.

  1. package com.techfundaes.memcacheBag;  
  2.   
  3. import java.net.InetSocketAddress;  
  4. import java.util.Date;  
  5.   
  6. import net.spy.memcached.MemcachedClient;  
  7.   
  8. public class StoreObjectsInCache  
  9. {  
  10.     public static void main(String[] args) throws Exception  
  11.     {  
  12.         MemcachedClient memcacheClient = new MemcachedClient(new InetSocketAddress("localhost"11211));  
  13.           
  14.         Object objectToCache = new Object();  
  15.         memcacheClient.set("keyObject"3600, objectToCache);  
  16.           
  17.         Date startDate = new Date();  
  18.         memcacheClient.set("keyDate"3600, startDate);  
  19.     }  
  20. }  
  21.    

Resource Link:

No comments:

Post a Comment