Friday, January 13, 2017

Memcached installation and sample code run

Memcached installation and run and check:
----------------------------------------
Installation:
-------------
$sudo apt-get update
$sudo apt-get install memcached

Check for status:
----------------
$ps aux | grep memcached
Memcached is running on the default port 11211. So in your code, use 11211 as port number.

Use with another port:
---------------------
For using Memcached server on a different port, run the command given below −

$memcached -p 11111 -U 11111 -u user -d

It should start the server and listen on TCP port 11111 and UDP port 11111 as a daemon process.

This command is explained below −

    -p is for TCP port number
    -U is for UDP port number
    -u is for user name
    -d runs memcached as daemon process

You can run multiple instances of Memcached server through a single installation.

Required Jar:
------------
<!-- https://mvnrepository.com/artifact/net.spy/spymemcached -->
<dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.12.1</version>
</dependency>

You can also download and use it in your build path of eclipse.


Run command:
-----------
$telnet HOST PORT

Here, HOST and PORT are machine IP and port number respectively, on which the Memcached server is running.
$telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.


Sample Code:
-----------
package com.rizvi.ProjectApps;

import java.util.HashMap;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;

public class MemcachedJavaClient {

    /**
     * MemcachedJavaClient program to show the usage of different functions that
     * can be performed on Memcached server with Java Client
     *
     * @param args
     */
    public static void main(String[] args) {
        // initialize the SockIOPool that maintains the Memcached Server
        // Connection Pool
        String[] servers = { "localhost:11211" };
        SockIOPool pool = SockIOPool.getInstance("Test1");
        pool.setServers(servers);
        pool.setFailover(true);
        pool.setInitConn(10);
        pool.setMinConn(5);
        pool.setMaxConn(250);
        pool.setMaintSleep(30);
        pool.setNagle(false);
        pool.setSocketTO(3000);
        pool.setAliveCheck(true);
        pool.initialize();
        // Get the Memcached Client from SockIOPool named Test1
        MemCachedClient mcc = new MemCachedClient("Test1");
        // add some value in cache
        System.out.println("add status:" + mcc.add("1", "Original"));
        System.out.println("mcc is: " + mcc.getCounter("Test1"));
        // Get value from cache
        System.out.println("Get from Cache:" + mcc.get("1"));

        System.out.println("add status:" + mcc.add("1", "Modified"));
        System.out.println("Get from Cache:" + mcc.get("1"));

        // use set function to add/update value, use replace to update and not
        // add
        System.out.println("set status:" + mcc.set("1", "Modified"));
        System.out.println("Get from Cache after set:" + mcc.get("1"));

        // use delete function to delete key from cache
        System.out.println("remove status:" + mcc.delete("1"));
        System.out.println("Get from Cache after delete:" + mcc.get("1"));

        // Use getMulti function to retrieve multiple keys values in one
        // function
        // Its helpful in reducing network calls to 1
        mcc.set("2", "2");
        mcc.set("3", "3");
        mcc.set("4", "4");
        mcc.set("5", "5");
        String[] keys = { "1", "2", "3", "INVALID", "5" };
        HashMap<String, Object> hm = (HashMap<String, Object>) mcc.getMulti(keys);

        for (String key : hm.keySet()) {
            System.out.println("KEY:" + key + " VALUE:" + hm.get(key));
        }
    }
}

Resource Link:

https://www.tutorialspoint.com/memcached/memcached_environment.htm

No comments:

Post a Comment