search
HomeBackend DevelopmentPHP TutorialDetailed explanation of php distributed deployment examples
Detailed explanation of php distributed deployment examplesMar 12, 2018 am 11:29 AM
phpdistributedDetailed explanation

In ordinary Web development, the common mode is that after the user logs in, the login status information is stored in the Session, some of the user's commonly used hot data is stored in the file cache, and the attachment information uploaded by the user is stored in a directory on the Web server. . This method is very convenient to use and fully capable for general Web applications. But for high-concurrency enterprise-level websites, it cannot handle it. Web clusters need to be used to achieve load balancing.

After deploying using the Web cluster method, the first thing to adjust is the user status information and attachment information. User status can no longer be saved to the Session, the cache cannot use the file cache of the local Web server, and attachments cannot be saved on the Web server. Because it is necessary to ensure that the status of each web server in the cluster is completely consistent. Therefore, user status, cache, etc. need to be saved to a dedicated cache server, such as Memcache. Attachments need to be saved to cloud storage, such as Qiniu Cloud Storage, Alibaba Cloud Storage, Tencent Cloud Storage, etc.

This article takes the ThinkPHP development framework as an example to explain how to set up and save Session, cache, etc. to the Memcache cache server.

Download the cached Memcache processing class and place it in the Thinkphp\Extend\Driver\Cache directory; download the Session Memcache processing class and place it in the Thinkphp\Extend\Driver\Session directory. As shown in the figure below:


Modify the configuration file, adjust the Session and cache, and record them to the Memcache server. Open ThinkPHP\Conf\convention.PHP, add configuration items:

  1. /* Memcache缓存设置 */  
    'MEMCACHE_HOST'         => '192.168.202.20',  
    'MEMCACHE_PORT'         => 11211,

Modify the data cache to Memcache:

'DATA_CACHE_TYPE'
       => 
'Memcache'
,


Modify Session to Memcache:

  1. 'SESSION_TYPE'          => 'Memcache',

As shown below:


Because there are many types of cloud storage, attachments are stored on cloud storage, so I won’t go into details. Just parameterize the SDK provided by each cloud storage. After the above modifications, the Web server can be deployed in a distributed manner.

Attachment 1: CacheMemcache.class.php

<?php  
// +----------------------------------------------------------------------  
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]  
// +----------------------------------------------------------------------  
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.  
// +----------------------------------------------------------------------  
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )  
// +----------------------------------------------------------------------  
// | Author: liu21st <liu21st@gmail.com>  
// +----------------------------------------------------------------------  
  
defined(&#39;THINK_PATH&#39;) or exit();  
/** 
 * Memcache缓存驱动 
 * @category   Extend 
 * @package  Extend 
 * @subpackage  Driver.Cache 
 * @author    liu21st <liu21st@gmail.com> 
 */  
class CacheMemcache extends Cache {  
  
    /** 
     * 架构函数 
     * @param array $options 缓存参数 
     * @access public 
     */  
    function __construct($options=array()) {  
        if ( !extension_loaded(&#39;memcache&#39;) ) {  
            throw_exception(L(&#39;_NOT_SUPPERT_&#39;).&#39;:memcache&#39;);  
        }  
  
        $options = array_merge(array (  
            &#39;host&#39;        =>  C(&#39;MEMCACHE_HOST&#39;) ? C(&#39;MEMCACHE_HOST&#39;) : &#39;127.0.0.1&#39;,  
            &#39;port&#39;        =>  C(&#39;MEMCACHE_PORT&#39;) ? C(&#39;MEMCACHE_PORT&#39;) : 11211,  
            &#39;timeout&#39;     =>  C(&#39;DATA_CACHE_TIMEOUT&#39;) ? C(&#39;DATA_CACHE_TIMEOUT&#39;) : false,  
            &#39;persistent&#39;  =>  false,  
        ),$options);  
  
        $this->options      =   $options;  
        $this->options[&#39;expire&#39;] =  isset($options[&#39;expire&#39;])?  $options[&#39;expire&#39;]  :   C(&#39;DATA_CACHE_TIME&#39;);  
        $this->options[&#39;prefix&#39;] =  isset($options[&#39;prefix&#39;])?  $options[&#39;prefix&#39;]  :   C(&#39;DATA_CACHE_PREFIX&#39;);          
        $this->options[&#39;length&#39;] =  isset($options[&#39;length&#39;])?  $options[&#39;length&#39;]  :   0;          
        $func               =   $options[&#39;persistent&#39;] ? &#39;pconnect&#39; : &#39;connect&#39;;  
        $this->handler      =   new Memcache;  
        $options[&#39;timeout&#39;] === false ?  
            $this->handler->$func($options[&#39;host&#39;], $options[&#39;port&#39;]) :  
            $this->handler->$func($options[&#39;host&#39;], $options[&#39;port&#39;], $options[&#39;timeout&#39;]);  
    }  
  
    /** 
     * 读取缓存 
     * @access public 
     * @param string $name 缓存变量名 
     * @return mixed 
     */  
    public function get($name) {  
        N(&#39;cache_read&#39;,1);  
        return $this->handler->get($this->options[&#39;prefix&#39;].$name);  
    }  
  
    /** 
     * 写入缓存 
     * @access public 
     * @param string $name 缓存变量名 
     * @param mixed $value  存储数据 
     * @param integer $expire  有效时间(秒) 
     * @return boolen 
     */  
    public function set($name, $value, $expire = null) {  
        N(&#39;cache_write&#39;,1);  
        if(is_null($expire)) {  
            $expire  =  $this->options[&#39;expire&#39;];  
        }  
        $name   =   $this->options[&#39;prefix&#39;].$name;  
        if($this->handler->set($name, $value, 0, $expire)) {  
            if($this->options[&#39;length&#39;]>0) {  
                // 记录缓存队列  
                $this->queue($name);  
            }  
            return true;  
        }  
        return false;  
    }  
  
    /** 
     * 删除缓存 
     * @access public 
     * @param string $name 缓存变量名 
     * @return boolen 
     */  
    public function rm($name, $ttl = false) {  
        $name   =   $this->options[&#39;prefix&#39;].$name;  
        return $ttl === false ?  
            $this->handler->delete($name) :  
            $this->handler->delete($name, $ttl);  
    }  
  
    /** 
     * 清除缓存 
     * @access public 
     * @return boolen 
     */  
    public function clear() {  
        return $this->handler->flush();  
    }  
}

# Attachment 2: SessionMemcache.class.php

    <?php   
    // +----------------------------------------------------------------------  
    // |   
    // +----------------------------------------------------------------------  
    // | Copyright (c) 2013-   
    // +----------------------------------------------------------------------  
    // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )  
    // +----------------------------------------------------------------------  
    // | Author: richievoe <richievoe@163.com>  
    // +----------------------------------------------------------------------  
        /** 
         * 自定义Memcache来保存session 
         */  
    Class SessionMemcache{  
        //memcache对象  
        private $mem;  
        //SESSION有效时间  
        private $expire;  
        //外部调用的函数  
        public function execute(){  
            session_set_save_handler(  
                array(&$this,&#39;open&#39;),   
                array(&$this,&#39;close&#39;),   
                array(&$this,&#39;read&#39;),   
                array(&$this,&#39;write&#39;),   
                array(&$this,&#39;destroy&#39;),   
                array(&$this,&#39;gc&#39;)  
                );  
        }  
        //连接memcached和初始化一些数据  
        public function open($path,$name){  
            $this->expire = C(&#39;SESSION_EXPIRE&#39;) ? C(&#39;SESSION_EXPIRE&#39;) :ini_get(&#39;session.gc_maxlifetime&#39;);  
            $this->mem = new Memcache;  
            return $this->mem->connect(C(&#39;MEMCACHE_HOST&#39;), C(&#39;MEMCACHE_PORT&#39;));  
        }  
        //关闭memcache服务器  
        public function close(){  
            return $this->mem->close();  
        }  
        //读取数据  
        public function read($id){  
            $id = C(&#39;SESSION_PREFIX&#39;).$id;  
            $data = $this->mem->get($id);  
            return $data ? $data :&#39;&#39;;  
        }  
        //存入数据  
        public function write($id,$data){  
            $id = C(&#39;SESSION_PREFIX&#39;).$id;  
            //$data = addslashes($data);  
            return $this->mem->set($id,$data,0,$this->expire);  
        }  
        //销毁数据  
        public function destroy($id){  
            $id = C(&#39;SESSION_PREFIX&#39;).$id;  
            return $this->mem->delete($id);  
        }  
        //垃圾销毁  
        public function gc(){  
            return true;  
        }  
    }  
    ?>
Related recommendations:

Detailed explanation of distributed deployment examples for ThinkPHP projects

hadoop 2.6.0 Pseudo-distributed deployment installation example tutorial

PHP extended Memcache distributed deployment solution_PHP

The above is the detailed content of Detailed explanation of php distributed deployment examples. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
PHP实现开源SeaweedFS分布式文件系统PHP实现开源SeaweedFS分布式文件系统Jun 18, 2023 pm 03:56 PM

在分布式系统的架构中,文件管理和存储是非常重要的一部分。然而,传统的文件系统在应对大规模的文件存储和管理时遇到了一些问题。为了解决这些问题,SeaweedFS分布式文件系统被开发出来。在本文中,我们将介绍如何使用PHP来实现开源SeaweedFS分布式文件系统。什么是SeaweedFS?SeaweedFS是一个开源的分布式文件系统,它用于解决大规模文件存储和

Pandas 与 PySpark 强强联手,功能与速度齐飞!Pandas 与 PySpark 强强联手,功能与速度齐飞!May 01, 2023 pm 09:19 PM

​使用Python做数据处理的数据科学家或数据从业者,对数据科学包pandas并不陌生,也不乏像云朵君一样的pandas重度使用者,项目开始写的第一行代码,大多是importpandasaspd。pandas做数据处理可以说是yyds!而他的缺点也是非常明显,pandas只能单机处理,它不能随数据量线性伸缩。例如,如果pandas试图读取的数据集大于一台机器的可用内存,则会因内存不足而失败。另外​pandas在处理大型​数据方面非常慢,虽然有像Dask或Vaex等其他库来优化提升数

PHP中的分布式数据中心PHP中的分布式数据中心May 23, 2023 pm 11:40 PM

随着互联网的快速发展,网站的访问量也在不断增长。为了满足这一需求,我们需要构建高可用性的系统。分布式数据中心就是这样一个系统,它将各个数据中心的负载分散到不同的服务器上,增加系统的稳定性和可扩展性。在PHP开发中,我们也可以通过一些技术实现分布式数据中心。分布式缓存分布式缓存是互联网分布式应用中最常用的技术之一。它将数据缓存在多个节点上,提高数据的访问速度和

使用Redis实现分布式计数器使用Redis实现分布式计数器May 11, 2023 am 08:06 AM

什么是分布式计数器?在分布式系统中,多个节点之间需要对共同的状态进行更新和读取,而计数器是其中一种应用最广泛的状态之一。通俗地讲,计数器就是一个变量,每次被访问时其值就会加1或减1,用于跟踪某个系统进展的指标。而分布式计数器则指的是在分布式环境下对计数器进行操作和管理。为什么要使用Redis实现分布式计数器?随着分布式计算的普及,分布式系统中的许多细节问题也

分布式系统必须知道的一个共识算法:Raft分布式系统必须知道的一个共识算法:RaftApr 07, 2023 pm 05:54 PM

一、Raft 概述​​Raft 算法​​​是分布式系统开发首选的​​共识算法​​。比如现在流行 Etcd、Consul。如果​​掌握​​​了这个算法,就可以较容易地处理绝大部分场景的​​容错​​​和​​一致性​​需求。比如分布式配置系统、分布式 NoSQL 存储等等,轻松突破系统的单机限制。Raft 算法是通过一切以领导者为准的方式,实现一系列值的共识和各节点日志的一致。二、Raft 角色2.1 角色跟随者(Follower):​​普通群众​​,默默接收和来自领导者的消息,当领导者心跳信息超时的

Redis实现分布式配置管理的方法与应用实例Redis实现分布式配置管理的方法与应用实例May 11, 2023 pm 04:22 PM

Redis实现分布式配置管理的方法与应用实例随着业务的发展,配置管理对于一个系统而言变得越来越重要。一些通用的应用配置(如数据库连接信息,缓存配置等),以及一些需要动态控制的开关配置,都需要进行统一管理和更新。在传统架构中,通常是通过在每台服务器上通过单独的配置文件进行管理,但这种方式会导致配置文件的管理和同步变得十分复杂。因此,在分布式架构下,采用一个可靠

Redis实现分布式对象存储的方法与应用实例Redis实现分布式对象存储的方法与应用实例May 10, 2023 pm 08:48 PM

Redis实现分布式对象存储的方法与应用实例随着互联网的快速发展和数据量的快速增长,传统的单机存储已经无法满足业务的需求,因此分布式存储成为了当前业界的热门话题。Redis是一个高性能的键值对数据库,它不仅支持丰富的数据结构,而且支持分布式存储,因此具有极高的应用价值。本文将介绍Redis实现分布式对象存储的方法,并结合应用实例进行说明。一、Redis实现分

PHP与数据库分布式的集成PHP与数据库分布式的集成May 15, 2023 pm 09:40 PM

随着互联网技术的发展,对于一个网络应用而言,对数据库的操作非常频繁。特别是对于动态网站,甚至有可能出现每秒数百次的数据库请求,当数据库处理能力不能满足需求时,我们可以考虑使用数据库分布式。而分布式数据库的实现离不开与编程语言的集成。PHP作为一门非常流行的编程语言,具有较好的适用性和灵活性,这篇文章将着重介绍PHP与数据库分布式集成的实践。分布式的概念分布式

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version