memcache operation class

Time:2022-11-24

1. Sample code

<?php
namespace Common\Drive;

use \Memcache;

/**
 * Memcache cache operation class
 */
class Mcache 
{
    protected static $_instance = null;
    private $mcache;
    const CACHE_KEY     = 'userinfo';

    /**
     * Singleton instance
     *
     * @return Object
     */
    public static function getInstance($dbHost, $port)
    {
        if (self::$_instance === null) {
            self::$_instance = new self($dbHost, $port);
        }
        return self::$_instance;
    }

    /**
     * Link Memcache cache
     */
    public function __construct ($host='127.0.0.1', $port='11211')
    {
        if($this->mcache) return $this->mcache;
        $this->mcache = new Memcache();
        $this->mcache ->connect($host, $port);
    }

    /**
     * Get key value
     * @param $key
     * @return mixed
     */
    public function get($keyssss)
    {
        $key = md5(static::CACHE_KEY.$keyssss);
        return $this->mcache->get($key);
    }


    /**
     * Set the key value
     * @param $key
     * @param $str
     * @return mixed
     */
    public function set($keyssss, $str, $time=60)
    {
        $key = md5(static::CACHE_KEY.$keyssss);
// echo 'Settings'.$keyssss.'===='.$key.PHP_EOL;
        return $this->mcache->set($key, $str, 0, $time);
    }


    /**
     * Get user information
     * @param $key
     * @return array|string
     */
    public function getUserInfo($key)
    {
        return $this->mcache->get($key);
    }

}

2. Add a memcache service

Order:
/usr/local/memcached/bin/memcached -d -m 200 -u root -p 12345 -c 1000 -P /tmp/memcached.pid

The d option is to start a daemon process,
-m is the amount of memory allocated to Memcache in MB, here is 200MB
-u is the user running Memcache. If you are currently root, you need to use this parameter to specify the user.
-l is the IP address of the listening server. If there are multiple addresses, I specified the IP address of the server 192.168.1.91 here.
-p is to set the port that Memcache listens to. I set 12301 here, preferably a port above 1024
The -c option is the maximum number of concurrent connections running, the default is 1024, and 256 is set here
-P is set to save the pid file of Memcache, here I save it in /tmp/memcached.pid

Stop the Memcache process:
# kill `cat /tmp/memcached.pid`
You can also start multiple daemons, but the ports cannot be repeated

-d install install memcached
-d uninstall uninstall memcached
-d start Start the memcached service
-d restart Restart the memcached service
-d stop stop memcached service
-d shutdown stop memcached service

1. View the started memcache service:
   netstat -lp | grep memcached

2. Check the process number of memcache (according to the process number, you can end the memcache service: "kill -9 process number")
   ps -ef | grep memcached

Recommended Today

META-INF/versions/9/module-info.class conflict resolution

I recently transformed the automation project and wanted to introduce a foreign automation framework: selenide.It was found that after the jar package was introduced, package conflicts were reported all the time.Look at the exception thrown by a conflict detection plug-in of maven <groupId>org.apache.maven.plugins</groupId> <artifactId >maven-enforcer-plugin</artifactId> <version>1.4.1</version> The specific error is as follows: NFO] Adding ignore: […]