A detailed discussion of PHP distributed deployment

小云云
Release: 2023-03-19 16:08:01
Original
6711 people have browsed it

In this article, we will share with you PHP distributed deployment, hoping that everyone will have a clearer idea about PHP distributed deployment.

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 certain location of the Web server. on a directory. 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, All are recorded on the Memcache server. Open ThinkPHP\Conf\convention.PHP and add configuration items:




##[ php]

view plain copy


  1. ##/* Memcache cache settings */

  2. 'MEMCACHE_HOST'

    =>'192.168.202.20',

  3. ##'MEMCACHE_PORT'
  4. ## Modify the data cache to Memcache:



    [php]view plain copy


    1. ##'DATA_CACHE_TYPE'   =>'Memcache',

    ## Modify Session to Memcache:





    ##[php]

    view plain copy


      ##'SESSION_TYPE'
    1.     =>'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]

    view plain copy

    1. // +----------------------------------------------------------------------

    2. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]

    3. // +----------------------------------------------------------------------

    4. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.

    5. // +----------------------------------------------------------------------

    6. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

    7. // +----------------------------------------------------------------------

    8. // | Author: liu21st

    9. // +----------------------------------------------------------------------

    10. defined('THINK_PATH')orexit();

    11. ##/**

    12. ## * Memcache cache driver

    13. * @category Extend

    14. ## * @package Extend

    15. * @subpackage Driver.Cache

    16. ## * @author liu21st
    17. */
    18. ##class
    19. CacheMemcache

      extendsCache {

    20. /**
    21. * Architectural function
    22. ## * @param array $options Cache parameters

    23. * @access public

    24. */

    25. function__construct($options=array()) {

    26. if( !extension_loaded('memcache') ) {

    27. throw_exception(L('_NOT_SUPPERT_').':memcache');

    28. }

    29. $options=array_merge(array(

    30. 'host'=> C('MEMCACHE_HOST') ? C('MEMCACHE_HOST') :'127.0.0.1',

    31. 'port'=> C('MEMCACHE_PORT') ? C('MEMCACHE_PORT') : 11211,

    32. 'timeout'=> C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,

    33. 'persistent'=> false,

    34. ),$options);

    35. $this->options =$options;

    36. $this->options['expire'] = isset($options['expire'])?$options['expire'] : C('DATA_CACHE_TIME');

    37. $this->options['prefix'] = isset($options['prefix'])?$options['prefix'] : C('DATA_CACHE_PREFIX');

    38. $this->options['length'] = isset($options['length'])?$options['length'] : 0;

    39. $func=$options['persistent'] ?'pconnect':'connect';

    40. $this->handler =newMemcache;

    41. $options['timeout'] === false ?

    42. $this->handler->$func($options['host'],$options['port ']) :

    43. ####$func($options['host'],$options['port'],$options[' timeout']);## }

    44. /**

    45. ## * Read cache

    46. ## * @access public

    47. * @param string $name cache variable name

    48. ## * @return mixed

    49. ## */

    50. ##public

      functionget($name) {

      ## N(
    51. 'cache_read'
    52. ,1 ; handler->get($this->options['prefix'

    53. ].
    54. $name);## }#

    55. # * @access public

    56. * @param string $name Cache variable name

    57. ## * @param mixed $value Stored data

  5. * @param integer $expire 有效时间(秒)

  6. * @return boolen

  7. */

  8. publicfunctionset($name,$value,$expire= null) {

  9. N('cache_write',1);

  10. if(is_null($expire)) {

  11. $expire=$this->options['expire'];

  12. }

  13. $name=$this->options['prefix'].$name;

  14. if($this->handler->set($name,$value, 0,$expire)) {

  15. if($this->options['length']>0) {

  16. // 记录缓存队列

  17. $this->queue($name);

  18. }

  19. returntrue;

  20. }

  21. returnfalse;

  22. }

  23. /**

  24. ## * Delete cache

  25. * @access public

  26. ## * @param string $name cache variable name

  27. * @return boolen

  28. ##*/

  29. public

    functionrm($name,$ttl= false) {

  30. $name

    =$this->options['prefix'].$name;

  31. return$ttl=== false ?

  32. $this->handler->delete($name) :

  33. $this->handler->delete($name,$ttl);

  34. }

  35. /**

  36. * clear cache

  37. * @access public

  38. # * @return boolen

  39. */

  40. publicfunctionclear() {

  41. return$this->handler->flush();

  42. }

  43. }

  附件2:SessionMemcache.class.php




[php]view plain copy


  1. // +----------------------------------------------------------------------

  2. // |

  3. // +----------------------------------------------------------------------

  4. // | Copyright (c) 2013-

  5. // +----------------------------------------------------------------------

  6. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

  7. // +-------------------------------- -------------------------------------

  8. ##// | Author: richievoe

  9. ##// +----------------------------------- ----------------------------------

  10. ##
  11. /**

  12. ## * Customize Memcache to save session
  13. */
  14. ## Class SessionMemcache{

  15. ##//memcache object

  16. private

  17. $mem;#//SESSION valid time

  18. ##private

  19. $expire

    ;##//Externally called functions

  20. publicfunctionexecute(){

  21. session_set_save_handler(

  22. array(&$this,'open'),

  23. array(&$this,'close'),

  24. array(&$this,'read'),

  25. array(&$this,'write'),

  26. array(&$this,'destroy'),,

    'gc'
  27. )

    ### } }##//Connect memcached and initialize some data

  28. public

  29. function
  30. open($ path,$name

  31. ){
  32. ##$this->expire = C('SESSION_EXPIRE') ? C('SESSION_EXPIRE') :ini_get

    (
  33. 'session.gc_maxlifetime'

    );

  34. ###return$this

    ->mem->connect(C(
  35. 'MEMCACHE_HOST'

    ), C('MEMCACHE_PORT'));}

    //Close memcache server
  36. publicfunction

    close(){
  37. ##}

  38. ##

    //Read datapublic

  39. functionread(

  40. $id
  41. ){

  42. $id= C('SESSION_PREFIX').$id;

  43. $data=$this->mem->get($id);

  44. return$data?$data:'';

  45. }

  46. //存入数据

  47. publicfunctionwrite($id,$data){

  48. $id= C('SESSION_PREFIX').$id;

  49. //$data = addslashes($data);

  50. return$this->mem->set($id,$data,0,$this->expire);

  51. }

  52. //销毁数据

  53. publicfunctiondestroy($id){

  54. $id= C('SESSION_PREFIX').$id;

  55. return$this->mem->delete($id);

  56. ## }

  57. //Garbage destruction

  58. ## publicfunctiongc(){

  59. # true;## }

  60. }

  61. ?>

  62. #Related recommendations:

PHP distributed tracing experience sharing

Issues related to PHP distributed and large-scale data processing

php distributed architecture

The above is the detailed content of A detailed discussion of PHP distributed deployment. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!