JAVA MemCache an unprecedented detailed explanation! After reading the bag proficient memcache! __java

Source: Internet
Author: User
Tags memcached memory usage

Memcach What is Memcache

Cache solution in Memcache cluster environment

Memcache is a high-performance distributed memory object caching system that can be used to store data in a variety of formats, including images, videos, files, and the results of database retrieval, by maintaining a large, unified hash table in memory. It simply means that the data is called into memory and then read from memory, thus greatly increasing the reading speed.

Memcache is a Danga project, the first LiveJournal service, originally developed to speed up livejournal access, and was later adopted by many large Web sites.

Memcached is run as a daemon on one or more servers, receiving client connections and operations at any time


Why would there be memcache and memcached two kinds of names

In fact, Memcache is the name of this project, and memcached is its server-side main program file name, you know what I mean. One is the project name, one is the main program file name, on the internet to see a lot of people do not understand, so mixed use.



Memcached is a high-performance, distributed memory object caching system for reducing database load and increasing access speed in dynamic applications. Memcached is developed by Danga Interactive to enhance the speed of livejournal.com access. LJ's dynamic page hits thousands of times per second, user 7 million. Memcached reduces database load significantly, allocates resources better, and accesses more quickly.


The internet Baidu a lot of things, almost all the same, and based on Java is very little, all only in the study of various other language classes and then try to do a simple operation in Java. First from the memcached, the latest version of Memcached is developed and designed in C language, it is said that the old version was developed in the Perl language, and it is an application software, as the server side of the cache server running on the server, You need to write clients in a specific language to communicate with them for caching and fetching data. Typically, we're running the memcached installation on a Web server, then by caching the required data, as far as I know, all the data cache settings and access operations, as well as the update of the data after the replacement operation of all the need for the program to do, rather than automatic (automatically do not know whether the success, hehe). The following is a practical example to apply memcached.

How to download the words to the http://danga.com/memcached/download memcached.


Ubuntu install memcached before compiling, please confirm that gcc, make, patch and other compile tools are installed, and can be used normally.

Install Libevent

Libevent is an asynchronous event-processing software library that is released in BSD licenses. Memcached relies on libevent, so the installation libevent must be compiled first.


Detect the success of libevent installation, enter: # Ls-al/usr/lib | The grep libevent will produce the following results (different machines may have different outputs):

yangfei@yangfei-laptop:~$ ls-al/usr/lib |grep libevent
lrwxrwxrwx 1 root 2009-07-19 08:45 libevent-1.4.so.2-> libevent-1.4.so.2.1.3
-rwxr-xr-x 1 root root 301588 2009-07-19 08:45 libevent-1.4.so.2.1.3
-rw-r--r--1 root root 386638 2009-07-19 08:45 libevent.a
lrwxrwxrwx 1 root 2009-07-19 08:45 libevent_core-1.4.so.2-> libevent_core-1.4.so.2.1.3
-rwxr-xr-x 1 root root 115721 2009-07-19 08:45 libevent_core-1.4.so.2.1.3
-rw-r--r--1 root root 151618 2009-07-19 08:45 libevent_core.a
-rwxr-xr-x 1 root root 860 2009-07-19 08:45 libevent_core.la
lrwxrwxrwx 1 root 2009-07-19 08:45 libevent_core.so-> libevent_core-1.4.so.2.1.3
lrwxrwxrwx 1 root 2009-07-19 08:45 libevent_extra-1.4.so.2-> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 239933 2009-07-19 08:45 libevent_extra-1.4.so.2.1.3
-rw-r--r--1 root root 298406 2009-07-19 08:45 libevent_extra.a
-rwxr-xr-x 1 root root 867 2009-07-19 08:45 libevent_extra.la
lrwxrwxrwx 1 root 2009-07-19 08:45 libevent_extra.so-> libevent_extra-1.4.so.2.1.3
-rwxr-xr-x 1 root root 825 2009-07-19 08:45 libevent.la
lrwxrwxrwx 1 root 2009-07-19 08:45 libevent.so-> libevent-1.4.so.2.1.3


?
View Code BASH

1
2
3
4
5
wget http://www.monkey.org/~provos/libevent-2.0.13-stable.tar.gz
tar xzvf  libevent-2.0.13-stable.tar.gz
./configure make make
install


1) Install Memcache service End

sudo apt-get install memcached

After installing the Memcache server, we need to start the service:

Memcached-d-M 128-p 11111-u Root


Here you need to explain the startup parameters for the memcached service:

-P Listening Port
The IP address of the-l connection, the default is native
-D Start memcached service
-D Restart Restart memcached service
-D Stop|shutdown shut down the running memcached service
-D Install installation memcached service
-d Uninstall Uninstall memcached service
-U Run as (only valid when run as root)
-m maximum memory usage, in MB. Default 64MB
-Returns an error when M memory is exhausted instead of deleting the item
-C Maximum Simultaneous connection number, default is 1024
-F Block size growth factor, default is 1.25-n minimum allocation space, key+value+flags default is 48
-H Display Help


See if success was established

Telnet test memcached
Telnet 192.168.1.2 11211

Trying 192.168.1.2 ...
Connected to 192.168.1.2.
Escape character is ' ^] '

View version

Version

...

For a status query on the memcached cache service, you can first telnet to the service: Telnet 127.0.0.1 11211, and then use the Stats command to view the status of the cached service, returning the following data:
time:1255537291 server's current UNIX timestamp
total_items:54 total number of items stored since server startup
Number of connection constructs that the CONNECTION_STRUCTURES:19 server assigns
version:1.2.6 memcache Version
limit_maxbytes:67108864 amount of memory allocated to Memcache (bytes)
cmd_get:1645 get Command (gets) the total number of requests
evictions:0 the number of items that are deleted to get free memory (the space allocated to Memcache should be full
To remove old items to get space allocated to new items)
TOTAL_CONNECTIONS:19 number of connections that have been opened since the server was started
bytes:248723 the number of bytes consumed by the current server storage items
Threads:1 the current number of threads
get_misses:82 Total missed Hits
POINTER_SIZE:32 the current operating system's pointer size (32-bit system is typically 32bit)
bytes_read:490982 Total Read bytes (number of bytes requested)
Number of seconds the uptime:161 server has been running
Curr_connections:18 the number of connections currently open
pid:2816 Memcache server's process ID
bytes_written:16517259 Total Bytes Sent (result bytes)
Total hit times of get_hits:1563
cmd_set:54 set Command (save) number of total requests
Number of items currently stored by the CURR_ITEMS:28 server

Ok, installation memcached1.4.5 successful.














Initialization: Memcache
Java code static {string[] ServerList = {"Server1.com:port", "Server2.com:port"};           Sockiopool pool = sockiopool.getinstance ();           Pool.setservers (serverlist);    Pool.initialize (); }
To create a client object:
Java code memcachedclient MC = new Memcachedclient ();
To create a cache:
Java code memcachedclient MC = new Memcachedclient ();    String key = "CacheKey1";    Object value = Someclass.getobject (); Mc.set (key, value);
Delete a cache via key:
Java code memcachedclient MC = new Memcachedclient ();    String key = "CacheKey1"; Mc.delete (key);
Gets the cached object via key:
Java code memcachedclient MC = new Memcachedclient ();    String key = "key"; Object value = Mc.get (key);
To get more than one cache object:
Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.