1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
 * Memcache<a href="
 */
classMCacheextendsObjectimplementsCacheFace
{
  private$mem= null;//Mem對象
     
  private$sId= 1; //servier服務(wù)ID
     
  /**
   * 初始化Memcache
   *
   * @return Object
   */
  publicfunction__construct()
  {
    if( !class_exists('Memcache') )
    {
      thrownewQException('PHP extension does not exist: Memcache');
    }
    $this->mem =newMemcache();
  }
     
  /**
   * 鏈接memcahce服務(wù)
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  string $value <a href="
   * @return array
   */
  privatefunctionconnect($sid)
  {
    $file=$this->CacheFile();
    require$file;
    if(! isset($cache) )
    {
      thrownewQException('<a href="
    }
    $server=$cache[$this->cacheId];
    $sid = isset($sid) == 0 ?$this->sId :$sid;//memcache服務(wù)選擇
    if( !$server[$sid])
    {
      thrownewQException('當(dāng)前操作的<a href="
    }
    $host=$server[$sid]['host'];
    $port=$server[$sid]['port'];
    try{
      $this->mem->connect($host,$port);
    }catch(Exception$e) {
      exit('memecache連接失敗,錯誤信息:'.$e->getMessage());
    }
  }
     
  /**
   * 寫入<a href="
   *
   * @access private
   * @param  string $key 關(guān)鍵字
   * @param  string $value <a href="
   * @return array
   */
  publicfunctionset($key,$value,$sid,$expire= 0)
  {
    $data=$this->get($key,$sid);//如果已經(jīng)存在key值
    if($data)
    {
      return$this->mem->set($key,$value,MEMCACHE_COMPRESSED ,$expire);
    }else{
      return$this->mem->add($key,$value,MEMCACHE_COMPRESSED ,$expire);
    }
  }
     
  /**
轉(zhuǎn)載請注明來源:php操作memcache緩存基礎(chǔ)方法

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。