Home > PHP Framework > Workerman > body text

How to use Memcached for data caching in Workerman

WBOY
Release: 2023-11-07 15:43:02
Original
824 people have browsed it

How to use Memcached for data caching in Workerman

How to use Memcached for data caching in Workerman

Memcached is an open source distributed memory caching system that is often used to improve the performance and scalability of web applications. Workerman is a high-performance PHP Socket framework that can be used to build real-time communication applications. This article will introduce how to use Memcached for data caching in Workerman and provide specific code examples.

1. Install and configure Memcached

Before starting, you need to install and configure Memcached. You can install Memcached on a Linux system through the following command:

sudo apt-get install memcached
Copy after login

After the installation is completed, you need to edit the configuration file /etc/memcached.conf, set the listening IP and port number, and specify the memory size.

-d
-p 11211
-l 127.0.0.1
-m 128
Copy after login

Save the configuration file and restart the Memcached service.

2. Install Workerman

Next, you need to install the Workerman framework. You can use Composer to install through the following command:

composer require workerman/workerman
Copy after login

3. Write the code to use Memcached

  1. Create a file named cache.php and write the following code:
<?php

require_once __DIR__.'/vendor/autoload.php';

use WorkermanWorker;
use WorkermanProtocolsHttp;

$worker = new Worker('http://0.0.0.0:8000');

$worker->onMessage = function ($connection, $request) {
    // 先尝试从缓存中获取数据
    $cache = new Memcached();
    $cache->addServer('127.0.0.1', 11211);
    $data = $cache->get($request->path());

    if ($data === false) {
        // 缓存中不存在数据,则从数据库中获取数据
        $data = get_data_from_database($request->path());

        // 将数据存入缓存
        $cache->set($request->path(), $data, 86400); // 缓存有效期为24小时
    }

    // 返回数据给客户端
    Http::header('Content-Type: application/json');
    Http::header('Cache-Control: max-age=86400'); // 设置浏览器缓存时间为24小时
    $connection->send(json_encode($data));
};

function get_data_from_database($path) {
    // 从数据库中获取数据的逻辑,此处省略
    return [
        'path' => $path,
        'data' => 'some data'
    ];
}

Worker::runAll();
Copy after login
  1. Execute the following command in the command line to start the service:
php cache.php start
Copy after login

4. Test code

can be sent using a browser or other tools HTTP request to test the data caching function of Memcached. For example, if you access http://localhost:8000/foo, the data will be fetched from the database and stored in the cache. Access http://localhost:8000/foo again, and the data will be obtained directly from the cache.

Through the above steps, we successfully used Memcached for data caching in Workerman. The examples in the code are only for reference and need to be adjusted according to the specific business logic when used in practice. At the same time, you need to pay attention to protecting the security of the Memcached service to avoid malicious operations by unauthorized visitors.

The above is the detailed content of How to use Memcached for data caching in Workerman. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template