Home Backend Development PHP Tutorial php memcache singleton

php memcache singleton

Jul 30, 2016 pm 01:30 PM
get gt mem val

  1. First set __construct() to private so that new is not allowed externally
  2. Instantiate an object inside the class. As long as it is available externally
<code><span><span>class</span><span>Mem</span><span>extends</span><span>Memcache</span>{</span><span>private</span><span>static</span><span>$mem_instance</span> = <span>null</span>;
    <span>private</span><span><span>function</span><span>__construct</span><span>()</span>{</span><span>parent</span>::__construct();
    }
    <span>private</span><span><span>function</span><span>__clone</span><span>()</span>{</span>}
    <span>public</span><span>static</span><span><span>function</span><span>get_mem_ins</span><span>()</span>{</span><span>if</span>(<span>null</span> === <span>self</span>::<span>$mem_instance</span>){
            <span>self</span>::<span>$mem_instance</span> = <span>new</span> Memcache;
        }
        <span>return</span><span>self</span>::<span>$mem_instance</span>;
    }
}</code>
Copy after login
  1. memcache operation
<code><span><?</span>php
    <span>//连接</span><span>$mem</span><span>=</span><span>new</span> Memcache;
    <span>$mem</span><span>-></span>connect(<span>"db.nowamagic.net"</span>, <span>12000</span>);
    <span>//保存数据</span><span>$mem</span><span>-></span><span>set</span>(<span>'key1'</span>, <span>'This is first value'</span>, <span>0</span>, <span>60</span>);
    <span>$val</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key1'</span>);
    echo <span>"Get key1 value: "</span><span>.</span><span>$val</span><span>.</span><span>"<br />"</span>;
    <span>//替换数据</span><span>$mem</span><span>-></span>replace(<span>'key1'</span>, <span>'This is replace value'</span>, <span>0</span>, <span>60</span>);
    <span>$val</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key1'</span>);
    echo <span>"Get key1 value: "</span><span>.</span><span>$val</span><span>.</span><span>"<br />"</span>;
    <span>//保存数组</span><span>$arr</span><span>=</span><span>array</span>(<span>'aaa'</span>, <span>'bbb'</span>, <span>'ccc'</span>, <span>'ddd'</span>);
    <span>$mem</span><span>-></span><span>set</span>(<span>'key2'</span>, <span>$arr</span>, <span>0</span>, <span>60</span>);
    <span>$val2</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key2'</span>);
    echo <span>"Get key2 value: "</span>;
    print_r(<span>$val2</span>);
    echo <span>"<br />"</span>;
    <span>//删除数据</span><span>$mem</span><span>-></span>delete(<span>'key1'</span>);
    <span>$val</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key1'</span>);
    echo <span>"Get key1 value: "</span><span>.</span><span>$val</span><span>.</span><span>"<br />"</span>;
    <span>//清除所有数据</span><span>$mem</span><span>-></span>flush();
    <span>$val2</span><span>=</span><span>$mem</span><span>-></span>get(<span>'key2'</span>);
    echo <span>"Get key2 value: "</span>;
    print_r(<span>$val2</span>);
    echo <span>"<br />"</span>;
    <span>//关闭连接</span><span>$mem</span><span>-></span>close();
<span>?></span><span></span></code>
Copy after login
  1. output
<code>Get key1 <span>value</span>: This <span>is</span> first <span>value</span>
Get key1 <span>value</span>: This <span>is</span> replace <span>value</span>
Get key2 <span>value</span>: Array ( [<span>0</span>] => aaa [<span>1</span>] => bbb [<span>2</span>] => ccc [<span>3</span>] => ddd )
Get key1 <span>value</span>:
Get key2 <span>value</span>:</code>
Copy after login

Copyright statement: Knowledge comes from the people, and is used by the people! Reprinting is welcome. Please attach a link to this article at the beginning. The article will be updated from time to time!

The above introduces the php memcache singleton, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to automate tasks using PowerShell How to automate tasks using PowerShell Feb 20, 2024 pm 01:51 PM

If you are an IT administrator or technology expert, you must be aware of the importance of automation. Especially for Windows users, Microsoft PowerShell is one of the best automation tools. Microsoft offers a variety of tools for your automation needs, without the need to install third-party applications. This guide will detail how to leverage PowerShell to automate tasks. What is a PowerShell script? If you have experience using PowerShell, you may have used commands to configure your operating system. A script is a collection of these commands in a .ps1 file. .ps1 files contain scripts executed by PowerShell, such as basic Get-Help

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How does java initiate an http request and call the post and get interfaces? How does java initiate an http request and call the post and get interfaces? May 16, 2023 pm 07:53 PM

1. Java calls post interface 1. Use URLConnection or HttpURLConnection that comes with java. There is no need to download other jar packages. Call URLConnection. If the interface response code is modified by the server, the return message cannot be received. It can only be received when the response code is correct. to return publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt

Example of Curl Get command Example of Curl Get command Mar 20, 2024 pm 06:56 PM

In Linux, URL or Curl client is a popular command line utility that allows you to transfer data over the network using various protocols such as HTTPS, HTTP, FTP, etc. It allows you to send and receive data using its get, post and request methods. Among them, you need to use the &quot;get&quot; method frequently. Therefore, it becomes crucial to learn various methods and various options that you can use to increase your productivity. &quot;Performing a curl operation is as simple as entering a few simple commands. Although it seems simple, many users do not fully realize its potential. Therefore, this short guide provides some information on how to perform curl operations on Linux systems. Example using the &quot;curlget&quot; command.&quot; Curl

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

How to change Ubuntu's apt-get update source? How to change Ubuntu's apt-get update source? Jan 05, 2024 pm 03:40 PM

Manually modify Ubuntu's apt-get source 1. Use the ssh tool to connect to Ubuntu (I use xshell) 2. Type cd/etc/apt/3 on the command line and back up the source.list file in this directory (you must have sudo permissions) ), then there is a source.list.bak file. 4. Clear the source.list file content (note: it cannot be restored after clearing, so you need to perform the previous step to back up the file in advance). At this time, use sudo to prompt that the permissions are insufficient. Switch directly to the root user and execute this command. 5. Use vim to open source.list, press the i key to enter the editing mode, paste the source address to be modified, and then press

See all articles