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

How to use the php memcached function

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use the php memcached function". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The usage of php memcached: 1, start Memcached;2, use the ps command to check the running status; 3, create a Memcached object; 4, add data through add and get methods.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

What is the use of php memcached?

Use PHP to simply manipulate Memcached:

Remember to start Memcached first!

[root@localhost] # / usr/bin/memcached-d-l 127.0.0.1-p 11211-m 150-u root

-d daemon mode (keeps the program running after exiting the terminal window),-l specifies the IP address 127.0.0.1,-p specifies how much memory (in M) is allocated to memcached by the port number 11211, and-u specifies which user to use to start memcached

Use the ps command to check the running status:

[root@localhost ~] # ps-ef | grep memcached

Show that the operation is successful!

Then you start using PHP to manipulate Memcached!

1. Create a Memcached object

M = new Memcached ()

two。 Pass in one or more servers

/ / pass in a server $m-> addServer ('127.0.0.1coach coach 11211'); / / pass in multiple servers $servers = array (array (' 127.0.0.1coach journal 11211'), array ('127.0.0.2command journal 11211'); $m-> addServers ($servers); / / check the running status print_r ($m-> getStats ())

Because the second server passed in does not exist, the correct information cannot be obtained.

You can also use getVersion () to get the version information of memcached directly.

3.add () method and get () method

/ * * add () * key represents the key value added to the cache * value represents the value value added to the cache * 600 represents the effective time of the cache for 600 seconds, 0 indicates permanent effect * / $m-> add ('key','value',600); / * get () * obtains the cache through the key value * / echo "cache key value:. $m-> get (' key)

If I add another one under the first add () method:

M-> add ('key','value1',600)

In fact, this will not overwrite the previous value, if you want to overwrite, you can use the replace () method

4.replace () method

M-> add ('key','value',600); $m-> replace (' key','value11',600)

This way we will get the value11 when we get ('key') again.

5.set () method

The set () method actually integrates the add method and the replace method. If the key value of set does not exist, it is equivalent to the add method. If the key value of set already exists, it is equivalent to the replace method, so generally speaking, the set method is the most commonly used method.

M-> set ('key','value',600)

6.delete () method

The method to delete the cache, using method:

/ * * delete () * pass a cached key * / $m-> delete ('key')

The cache expiration time I added above is not up yet. After executing the delete method, we still can't get this cache.

7.flush () method

Just get rid of all the cache! Use it with caution! )

8.increment () method

Addition operation, usage:

/ / set a cache with a key value of num,value to 5, permanently effective cache $m-> set ('num',5,0); / / add 5$ m-> increment (' num',5) to the cache value without refreshing the page with the key value of num; / / output this cache echo $m-> get ('num')

9.decrement () method

It is similar to the increment () method, except that it becomes subtraction, using the same method as the increment () method.

More elegant use of the Memcached method

10.setMulti () method

If set caching has been done according to the previous method, one entry at a time is very slow, so Memcached provides us with the setMulti () method, which is used as follows:

/ / first define the array $data = array ('key1'= >' value1', 'key2'= >' value2') to be cached; / * * setMulti () * the first parameter represents the passed array, the second parameter represents the effective time, and 0 represents the permanent * / $m-> setMulti ($data,0).

11.getMulti () method

Similarly, you can also obtain multiple caches at a time, using the following methods:

/ / define the key worth getting cached array $get = array ('key1','key2'); / * * getMulti () * pass in the key value to get the cache * / $result = $m-> getMulti ($get); var_dump ($result)

12.deleteMulti () method

Similar to the getMulti () method, except that the cache is deleted as follows:

$delete = array ('key1','key2'); $m-> deleteMulti ($delete)

Caches with key values of key1 and key2 are deleted.

13.getResultCode () method

When I have finished performing an operation, I can use this method to view the execution, using the following methods:

/ / I just executed the deleteMulti () method, at which time I can use the getResultCode () method to check whether the operation was successful or not echo $m-> getResultCode ()

When executed correctly, 0 will be put back.

For more status codes, please click: more status codes

14.getResultMessage () method

The difference from the getResultCode () method is that getResultCode () returns the status code, while the getResultMessage () method returns specific information. The method of use is as follows:

/ / execute the deleteMulti () method $m-> getResultMessage () above

This returns a SUCCESS message telling us that the operation is successful

That's all for "how to use the php memcached function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report