Showing posts with label Memcache. Show all posts
Showing posts with label Memcache. Show all posts

Sunday, February 18, 2018

How to install memcached in windows? Or [Memcached on Windows Failed to ignore SIGHUP: No error failed to daemon() in order to daemonize]

Memcached Download


Version: memcached-win64-1.4.4-14.zip

Then I followed 3 tutorials:

1. First follow this tutorial: https://imak47.wordpress.com/tag/failed-to-ignore-sighup/
2. Run the command below:
sc create "Memcached11211" binPath= "C:\memcached\memcached.exe -d runservice -p 11211" DisplayName= "Memcached11211" start= auto

3. Then we start them:
C:\memcached>sc start Memcached11211

SERVICE_NAME: Memcached11211
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 5412
        FLAGS              :

C:\memcached>sc start Memcached11212

SERVICE_NAME: Memcached11212
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
        PID                : 7976
        FLAGS              :

C:\memcached>netstat -an | grep 112
File STDIN:
  TCP    0.0.0.0:11211          0.0.0.0:0              LISTENING
  TCP    0.0.0.0:11212          0.0.0.0:0              LISTENING
  TCP    [::]:11211             [::]:0                 LISTENING
  TCP    [::]:11212             [::]:0                 LISTENING
  UDP    0.0.0.0:11211          *:*
  UDP    0.0.0.0:11211          *:*
  UDP    [::]:11211             *:*
  UDP    [::]:11211             *:*�
Note however that as configured, the udp port is still 11211, so it would need to be changed to ensure that udp can be used as well for both services.
You would add a -u 11211 and -u 11212 to the sc configuration lines.
To stop and individual memcached service you would use:
sc stop memcached11211
sc stop memcached11212
to remove the services do:
sc delete memcached11211
sc delete memcached11212
If however you're just trying it out on different ports then just use multiple cmd windows and run it that way.
Resource Link: 

Wednesday, November 22, 2017

Memcache flash command

Memcache flash command

echo 'flush_all' | netcat localhost 11211

Necessary Git Command:


git branch --v

git log

Tuesday, January 31, 2017

How to Install Memcached on CentOS 7

Memcached is a distributed, high-performance, in-memory caching system that is primarily used to speed up sites that make heavy use of databases. It can however be used to store objects of any kind. Nearly every popular CMS has a plugin or module to take advantage of memcached, and many programming languages have a memcached library, including PHP, Perl, Ruby, and Python. Memcached runs in memory and is thus quite speedy, since it does not need to write data to disk.
Pre-Flight Check
  • These instructions are intended specifically for installing Memcached on a single CentOS 7 node.
  • I’ll be working from a Liquid Web Core Managed CentOS 7 server, and I’ll be logged in as root.

Step #1 Install Memcached
First, clean-up yum:
yum clean all
As a matter of best practice we’ll update our packages:
yum -y update
Installing Memcached and related packages is now as simple as running just one command:
yum -y install memcached
Step #2: Configuration of the Memcached Installation
Use the following command to view information on the memcached command:
memcached -h
The default configuration file can be found at:
/etc/sysconfig/memcached
When started, Memcached will start on port 11211 by default per the default configuration file:
PORT=”11211″
USER=”memcached”
MAXCONN=”1024″
CACHESIZE=”64″
OPTIONS=””

To change the port (PORT), the user Memcached runs as (USER), the maximum number of allowed connections to Memcached (MAXCONN), or the cache size in megabytes (CACHESIZE), simply edit the configuration file.
For a refresher on editing files with vim see: New User Tutorial: Overview of the Vim Text Editor
EXAMPLE: If I wanted to run Memcached on port 1337, with 4GB of memory, and allow a maximum of 2,000 connections, I would change the config file as follows.
Let’s edit the configuration file:
vim /etc/sysconfig/memcached
To the following:
PORT=”1337″
USER=”memcached”
MAXCONN=”2000″
CACHESIZE=”4096″
OPTIONS=””

Exit and save the configuration file, and then restart Memcached
systemctl restart memcached
Step 3: Configure Memcached to Start on Boot
And then start Memcached:
systemctl start memcached
Be sure that Memcached starts at boot:
systemctl enable memcached
To check the status of Memcached:
systemctl status memcached
To stop Memcached:



systemctl stop memcached

Resource Link: https://www.liquidweb.com/kb/how-to-install-memcached-on-centos-7/

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:

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