1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | PHPSpider [ A PHP Framework For Crawler ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006-2014 https://doc.phpspider.org All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: Seatle Yang <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
|
12
|
|
|
//---------------------------------- |
13
|
|
|
// PHPSpider缓存类文件 |
14
|
|
|
//---------------------------------- |
15
|
|
|
|
16
|
|
|
class cache |
17
|
|
|
{ |
18
|
|
|
// 多进程下面不能用单例模式 |
19
|
|
|
//protected static $_instance; |
20
|
|
|
/** |
21
|
|
|
* 获取实例 |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
* @author seatle <[email protected]> |
25
|
|
|
* @created time :2016-04-10 22:55 |
26
|
|
|
*/ |
27
|
|
|
public static function init() |
28
|
|
|
{ |
29
|
|
|
if(extension_loaded('Redis')) |
30
|
|
|
{ |
31
|
|
|
$_instance = new Redis(); |
32
|
|
|
} |
33
|
|
|
else |
34
|
|
|
{ |
35
|
|
|
$errmsg = "extension redis is not installed"; |
36
|
|
|
log::add($errmsg, "Error"); |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
// 这里不能用pconnect,会报错:Uncaught exception 'RedisException' with message 'read error on connection' |
40
|
|
|
$_instance->connect($GLOBALS['config']['redis']['host'], $GLOBALS['config']['redis']['port'], $GLOBALS['config']['redis']['timeout']); |
41
|
|
|
|
42
|
|
|
// 验证 |
43
|
|
|
if ($GLOBALS['config']['redis']['pass']) |
44
|
|
|
{ |
45
|
|
|
if ( !$_instance->auth($GLOBALS['config']['redis']['pass']) ) |
46
|
|
|
{ |
47
|
|
|
$errmsg = "Redis Server authentication failed!!"; |
48
|
|
|
log::add($errmsg, "Error"); |
49
|
|
|
return null; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// 不序列化的话不能存数组,用php的序列化方式其他语言又不能读取,所以这里自己用json序列化了,性能还比php的序列化好1.4倍 |
54
|
|
|
//$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data |
55
|
|
|
//$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize |
56
|
|
|
//$_instance->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize |
57
|
|
|
|
58
|
|
|
$_instance->setOption(Redis::OPT_PREFIX, $GLOBALS['config']['redis']['prefix'] . ":"); |
59
|
|
|
|
60
|
|
|
return $_instance; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|