Simple use of memcached java client

Keywords: Spring Java xml Fragment

Simple use of memcached java client

Start Memcached server
./memcached -d -m10 -u root -l 192.168.1.106 -p 2222 -c 256 -P /tmp/memcached.pid
-The d option is to start a Daemons
-m is the amount of memory allocated to Memcache, in MB, here is 10MB
-u is the user running Memcache, here is r oot
-l is the IP address of the listening server. Here it is specified that the IP address of the server 192.168.1.106 - P is the listening port. Here, 2222 is set, preferably the port above 1024
-Option c is the maximum number of concurrent connections to run. The default value is 1024. 256 is set here
-P is the pi d file set to save Memcache. Here is the pi d file saved in / T MP / Memcache
There are several common ones that need to be understood:
-f block size growth factor, default is 1.25
-n minimum allocation space, key+val ue+f l ags is 48byt e - I by default, the size of each sl ab page
-v/ - vv detailed display of various parameters during operation
-Turn off Memcached. First use ps -ef| grep memcached to find the process number, and then kill it

1. Do not use the spring code fragment CacheHelper.java

    private static MemCachedClient mcc = new MemCachedClient();

    private CacheHelper() {
    }

    static {
        String[] servers ={ "127.0.0.1:11211" }; 
        //{ "192.168.1.106:2222","192.168.1.106:2223" };
        Integer[] weights = { 1};

        SockIOPool pool = SockIOPool.getInstance();

        pool.setServers(servers);
        pool.setWeights(weights);
        pool.setInitConn(5);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaxIdle(1000 * 60 * 60 * 6);
        pool.setMaintSleep(30);
        pool.setNagle(false);// Disable nagle algorithm
        pool.setSocketConnectTO(0);
        pool.setSocketTO(3000);// 3 seconds timeout
        pool.setHashingAlg(3);// Set to consistent hash algorithm

        pool.initialize();
    }

    public static MemCachedClient getMemCachedClient() {
        return mcc;
    }

        MemCachedClient mcc = CacheHelper.getMemCachedClient();

        //mcc.add("k2", "12345");
        UserModel um = new UserModel("11","11Name",11);
        mcc.set("t1", um);


        Object obj = mcc.get("k2");
            Object obj1 = mcc.get("t1");
        System.out.println(obj);
        System.out.println(obj1);

2. Integration with spring
applicationContext.xml file:

<!--memcached Client SocketPool-->    
    <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" 
    init-method="initialize" destroy-method="shutDown">      
        <constructor-arg><value>neeaMemcachedPool</value></constructor-arg>  
            <property name="servers">    
                <list>  
                <value>127.0.0.1:11211</value>
                <!-- 
                <value>192.168.1.106:2222</value>
                    <value>192.168.1.106:2223</value>
                 -->  
                </list>    
            </property>    
            <property name="weights">
                <list>    
                    <value>1</value>
                   <!-- <value>1</value> --> 
                </list>    
            </property>
            <property name="initConn">    
                <value>5</value>    
            </property>    
            <property name="minConn">    
                <value>5</value>    
            </property>
                <property name="maxConn">    
                <value>250</value>    
            </property>    
            <property name="maintSleep">    
                <value>30</value>    
            </property>    
            <property name="nagle">    
                <value>false</value>    
            </property>    
            <property name="maxIdle">    
                <value>6000</value>    
            </property>    
            <property name="socketTO">    
                <value>3000</value>    
            </property>    
        </bean>             
        <!--memcached client-->    
        <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient">    
            <constructor-arg>    
                <value>neeaMemcachedPool</value>    
            </constructor-arg>   
        </bean>
@Service
public class ClientTest {
    @Autowired
    private MemCachedClient mcc;
    public void setMcc(MemCachedClient mcc){
        this.mcc = mcc;
    }

    public static void main(String[] args) {
        //Do not use spring
//      MemCachedClient mcc = CacheHelper.getMemCachedClient();

        //mcc.add("k2", "12345");
//      UserModel um = new UserModel("11","11Name",11);
//      mcc.set("t1", um);


//      Object obj = mcc.get("k2");
//      Object obj1 = mcc.get("t1");
//      System.out.println(obj);
//      System.out.println(obj1);

//      List<UserModel> list = new ArrayList<UserModel>();
//      UserModel um1 = new UserModel("33","11Name",11);
//      UserModel um2 = new UserModel("44","22Name",22);
//      
//      list.add(um1);
//      list.add(um2);

//      ct.mcc.set("t1", list);
//      ct.mcc.set("k1","springt1");
//      
//      Object obj = ct.mcc.get("k1");
//      
//      System.out.println("obj==="+obj);       

        //Use and spring integration
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ClientTest ct = (ClientTest)ctx.getBean("clientTest");

        Object obj = ct.mcc.get("k2");
        Object obj1 = ct.mcc.get("t1");
        System.out.println(obj);
        System.out.println(obj1);

    }
}

Posted by parkej60 on Wed, 01 Apr 2020 10:24:20 -0700