Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Getting started with memcached

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Introduction to Memcache

What is Memcached?

Memcached is a high-performance, distributed memory object caching system developed by Danga Interactive.

It is used to reduce database load and improve access speed in dynamic applications.

What can Memcached cache?

By maintaining a unified large hash table in memory, Memcached can be used to store data in various formats

Including images, videos, files and database retrieval results.

Is Memcached fast?

Very fast. Memcached uses libevent (epoll, if possible, under linux) to balance any number of open links

Use the non-blocking network Istroke O to count references to internal objects (so objects can be in multiple states for multiple clients)

Use your own page block allocator and hash table, so virtual memory is not fragmented and the time complexity of virtual memory allocation is guaranteed to be O (1).

Danga Interactive has developed Memcached to speed up Danga Interactive.

Characteristics of Memcached

Memcached's cache is distributed and can be accessed by multiple users on different hosts at the same time.

Therefore, it solves the limitation that shared memory can only be used on a stand-alone computer, let alone when using a database to do similar things.

Disk overhead and blocking occurs.

Cache data:

1. Cache data in memory 2. The data form is key- > value structure 3. The degree of safety is very poor

Memcache installation

Operation under Windows

1 download memcached-1.2.1-win32.zip on the Internet. Decompress and put it under a disk, such as c:\ memcached

2 under the terminal (cmd):

D:\ AppServ > memcached.exe-d install installation

D:\ AppServ > memcached.exe-d uninstall uninstall

D:\ AppServ > memcached.exe-d start start

D:\ AppServ > memcached.exe-d stop stop

Memcached.exe-h get all the help

3 connect after startup:

D:\ AppServ > telnet 127.0.0.1 11211-connect memcache port 11211

Use quit to exit.

4 other command parameters:

Common parameters for starting Memcache

-p sets the port number (not set to: 11211 by default)

-U UDP listening port (default: 11211, closed at 0)

-l bind address (default: all are allowed, regardless of the internal and external network or local replacement of IP

There are security risks. If it is set to 127.0.0.1, it can only be accessed locally)

-d independent process running

...-d start starts the memcached service

...-d restart restarts the memcached service

...-d stop | shutdown shuts down the running memcached service

...-d install installs the memcached service

...-d uninstall uninstalls the memcached service

The-u binding uses the specified to run the process

-m maximum memory usage allowed, in M (default: 64 MB)

-P writes PID to a file, which makes the subsequent process terminate quickly. It needs to be used with-d.

-M returns an error when memory is exhausted instead of deleting an item

-c maximum number of simultaneous connections. Default is 1024.

-f block size growth factor, default is 1.25

-n minimum allocated space. Key+value+flags defaults to 48.

-h display help

PHP opens the Memcache module

1.phpinfo () detects whether there is a memcache extension

2.php extension directory extension_dir

Php_memcache.dll is placed under the php extension directory

3.php profile modification

Extension=php_memcache.dll

4. Restart the apache service

Installation operation under Linux:

2.1 # install memcache extensions for php environment

First download the extension package pecl-memcache-php7.zip supported by php7.0

Address: https://github.com/websupport-sk/pecl-memcache/archive/php7.zip

1. Transfer the pecl-memcache-php7.zip file to the server / root

two。 Enter this directory after decompressing the file

Unzip pecl-memcache-php7.zip

Cd pecl-memcache-php7

3. Because there is no configure configuration environment file in the directory

Execute / usr/local/php/bin/phpize to prepare a compiled environment

4. Configuration: plus configuration

. / configure-- with-php-config=/usr/local/php/bin/php-config

5. Make & & make install compilation and installation

6. A memcache.so file will be generated after the installation is successful

/ usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/

7. Modify php configuration file

Vim / usr/local/php/etc/php.ini

Add the following information

722 line extension_dir='/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/'

Extension= "memcache.so"

8. Just restart the apache service (check phpinfo ())

2.2 # install the memcache service

Memcached-1.4.4-3.el6.i686.rpm

a. First mount the ISO image file DVD1.ISO

Mount / dev/sr0 / mnt/cdrom/

b. Install memcached

Yum-y install memcached

c. Add memcache user

Useradd memcache

d. Start the memcache service

Memcached-d-M128-l 127.0.0.1-p 11211-u memcache

View Port

Netstat-tunpl | grep: 11211

Win+r opens a command line window connection test

Telnet 192.168.185.252 11211

Note:

If telnet is not an internal command when entering a telnet command

Windows system-> Control Panel-> programs and Features-> turn on and off Windows functionality-> check the telnet client

Stats memcache command: view current statu

e. Set Boot self-boot

Chkconfig memcached on

f. How to kill the background process

Pkill memcached

Memcache related operations

# check the status of stats

Process ID of the STAT pid 1552 service process

The elapsed time, in seconds, of the STAT uptime 3792 service from startup to the current time.

The time, in seconds, of the current system on the host where the STAT time 1262517674 server resides.

Version of the STAT version 1.2.6 component. Here is the 1.2.6 I am currently using.

The pointer size of the host operating system where the STAT pointer_size 32 server resides, usually 32 or 64. 0.

STAT curr_items 1 represents the number of all current cache objects. Objects that have been deleted from the cache are not included.

STAT total_items 2 represents the number of all objects that have been stored by the system since startup, including those that have been deleted.

STAT bytes 593 represents the storage space used by the system to store cached objects, in bytes.

STAT curr_connections 2 represents the number of connections currently open by the system.

STAT total_connections 28 represents the total number of connections opened by the system since the memcached service was started to the current time.

Set write cache

Set key name marks the length of valid time data

Set love 10 0 10

I love you

Key name

The maximum length is 250 characters.

Marking

Is a 16-bit unsigned integer that will be stored with the data to be stored and returned when the client get data

Effective time

In seconds

Data length

The unit is in bytes

Value of data

Maximum cannot exceed 1024kb

Get get cache

Get key name

Get love

# delete delete cache

Delete key name

Delete love

Flush_all clears all caches

# use of PHP and Memcache

/ / instantiate Memcache

$memcache = new Memcache ()

/ / Connect to Memcache

$memcache- > connect ('127.0.0.1)

$memcache- > addServer ('127.0.0.1)

/ / Save data

$memcache- > set ('name','xiaohigh',MEMCACHE_COMPRESSED,60)

/ / obtain data

$memcache- > get ('name')

/ / clear all data

$memcache- > flush ()

# PHP Memcahe class

Connect (ip, port) connects to a memcache server

AddServer (ip, port) add a memcache server

GetExtendedStats () gets all the server information in the connection pool

GetServerStatus (host, port) gets the connection status of the specified server

Add (key, value, tag, expires) add a piece of data

Replace (key, value, tag, expires) modifies a piece of data

Set (key, value, tag, expires) add or modify a piece of data

Get (key) gets the value based on the key

Delete (key) deletes a specified piece of data

Flush () delete all

Close () closes the connection

GetStats () gets the memcache server information and returns the array

GetVersion () gets the m emcache server version number

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report