├── CREDITS ├── EXPERIMENTAL ├── README.md ├── config.m4 ├── config.w32 ├── php_togo.h ├── tests └── 001.phpt ├── togo.c ├── togo.php ├── version └── php_togo_0.0.1.zip └── www └── test.php /CREDITS: -------------------------------------------------------------------------------- 1 | togo -------------------------------------------------------------------------------- /EXPERIMENTAL: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SKPHP1989/php_togo/4984f5d9af5464d617800988ac0b491f620b50f5/EXPERIMENTAL -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #PHP EXTENSION FOR TOGO 2 | 3 | #环境要求 4 | Linux,php5.4-5.6
5 | (ps:本人亲测,5.4,5.5,5.6版本是可以的!)
6 | 7 | #安装php_togo 8 | root@localhost#wget https://raw.githubusercontent.com/SKPHP1989/php_togo/master/version/php_togo_0.0.1.zip
9 | root@localhost#unzip php_togo_0.0.1.zip
10 | root@localhost#cd php_togo_0.0.1
11 | root@localhost#php-src/bin/phpize --with-php-config=php-src/bin/php-config
12 | root@localhost#./configure
13 | root@localhost#make && make install
14 |
15 | 修改php.ini文件 加载togo.so扩展库
16 | 重启apache或者php-fpm
17 |
18 | 检测是否已经加载togo
19 | root@localhost#php-src/bin/php -m|grep togo
20 | 出现togo说明已经加载
21 | 22 | ##togo地址:https://github.com/zhuli/togo 23 | 24 | 25 | ``` 26 | 27 | 功能: 28 | 1. 初始化以及常用说明 29 | 2. 计数器函数 30 | 3. 队列函数 31 | 4. 内存锁函数 32 | 33 | #1.初始化以及常用说明 34 | try{ 35 | $togo = new Togo; 36 | $togo->connect('127.0.0.1' ,8787 ,5); 37 | $togo->close(); 38 | }catch(TogoException $e){ 39 | var_dump($e) 40 | } 41 | ``` 42 | 43 | ##1 连接togo 44 | ### connect 45 | 46 | ##### *Paramer* 47 | 48 | *host*: string.
49 | *port*: int.
50 | *timeout*:int. 51 | 52 | ##### *Return value* 53 | 54 | *BOOL*: `TRUE` on success, `FALSE` on error. 55 | 56 | ##### *Example* 57 | 58 | ~~~ 59 | $togo->connect('127.0.0.1' ,8787 ,5); 60 | ~~~ 61 | 62 | ##2 获取togo版本 63 | ### version 64 | 65 | ##### *Paramer* 66 | 67 | ##### *Return value* 68 | 69 | *BOOL*: `STRING` on success, `FALSE` on error. 70 | 71 | ##### *Example* 72 | 73 | ~~~ 74 | $togo->version(); 75 | ~~~ 76 | 77 | ##3 关闭togo连接 78 | ### close 79 | 80 | ##### *Paramer* 81 | 82 | ##### *Return value* 83 | 84 | *BOOL*: `TRUE` on success, `FALSE` on error. 85 | 86 | ##### *Example* 87 | 88 | ~~~ 89 | $togo->close(); 90 | ~~~ 91 | 92 | #2.计数器函数: 93 | 94 | ##1 计数器加上某个数字,默认+1 95 | ### counter_plus 96 | 97 | ##### *Paramer* 98 | 99 | *name*: string.
100 | *value*: int, optional ,default 1. 101 | 102 | ##### *Return value* 103 | 104 | *BOOL*: `INT` on success, `FALSE` on error. 105 | 106 | ##### *Example* 107 | 108 | ~~~ 109 | $togo->counter_plus('test'); 110 | $togo->counter_plus('test' ,6); 111 | ~~~ 112 | 113 | ##2 计数器减去某个数字,默认-1 114 | ### counter_minus 115 | 116 | ##### *Paramer* 117 | 118 | *name*: string.
119 | *value*: int, optional ,default 1. 120 | 121 | ##### *Return value* 122 | 123 | *BOOL*: `INT` on success, `FALSE` on error. 124 | 125 | ##### *Example* 126 | 127 | ~~~ 128 | $togo->counter_minus('test'); 129 | $togo->counter_minus('test' ,6); 130 | ~~~ 131 | 132 | ##3 计数器获取一个值 133 | ### counter_get 134 | 135 | ##### *Paramer* 136 | 137 | *name*: string. 138 | 139 | ##### *Return value* 140 | 141 | *BOOL*: `INT` on success, `FALSE` on error. 142 | 143 | ##### *Example* 144 | 145 | ~~~ 146 | $togo->counter_get('test'); 147 | ~~~ 148 | 149 | ##4 计数器初始化 150 | ### counter_reset 151 | 152 | ##### *Paramer* 153 | 154 | *name*: string. 155 | 156 | ##### *Return value* 157 | 158 | *BOOL*: `TRUE` on success, `FALSE` on error. 159 | 160 | ##### *Example* 161 | 162 | ~~~ 163 | $togo->counter_reset('test'); 164 | ~~~ 165 | 166 | #3.队列消息函数: 167 | 168 | ##1 从左边插入一个记录 169 | ### queue_lpush 170 | 171 | ##### *Paramer* 172 | 173 | *name*: string.
174 | *value*: int.
175 | *priority*: int, optional. 176 | 177 | ##### *Return value* 178 | 179 | *BOOL*: `TRUE` on success, `FALSE` on error. 180 | 181 | ##### *Example* 182 | 183 | ~~~ 184 | $togo->queue_lpush('test_queue' ,1234); 185 | $togo->queue_lpush('test_queue' ,1234 ,2); 186 | ~~~ 187 | 188 | ##2 从右边插入一个记录 189 | ### queue_rpush 190 | 191 | ##### *Paramer* 192 | 193 | *name*: string.
194 | *value*: int.
195 | *priority*: int, optional. 196 | 197 | ##### *Return value* 198 | 199 | *BOOL*: `TRUE` on success, `FALSE` on error. 200 | 201 | ##### *Example* 202 | 203 | ~~~ 204 | $togo->queue_rpush('test_queue' ,1234); 205 | $togo->queue_rpush('test_queue' ,1234 ,2); 206 | ~~~ 207 | 208 | ##3 从左边获取一个记录 209 | ### queue_lpop 210 | 211 | ##### *Paramer* 212 | 213 | *name*: string. 214 | 215 | ##### *Return value* 216 | 217 | *BOOL*: `INT` on success, `FALSE` on error. 218 | 219 | ##### *Example* 220 | 221 | ~~~ 222 | $togo->queue_lpop('test_queue' ); 223 | ~~~ 224 | 225 | ##4 从右边获取一个记录 226 | ### queue_rpop 227 | 228 | ##### *Paramer* 229 | 230 | *name*: string. 231 | 232 | ##### *Return value* 233 | 234 | *BOOL*: `INT` on success, `FALSE` on error. 235 | 236 | ##### *Example* 237 | 238 | ~~~ 239 | $togo->queue_rpop('test_queue' ); 240 | ~~~ 241 | 242 | ##5 获取一个队列的总记录数 243 | ### queue_count 244 | 245 | ##### *Paramer* 246 | 247 | *name*: string. 248 | 249 | ##### *Return value* 250 | 251 | *BOOL*: `INT` on success, `FALSE` on error. 252 | 253 | ##### *Example* 254 | 255 | ~~~ 256 | $togo->queue_count('test_queue' ); 257 | ~~~ 258 | 259 | ##6 获取一个队列的状态 260 | ### queue_status 261 | 262 | ##### *Paramer* 263 | 264 | *name*: string. 265 | 266 | ##### *Return value* 267 | 268 | *BOOL*: `STRING` on success, `FALSE` on error. 269 | 270 | ##### *Example* 271 | 272 | ~~~ 273 | $togo->queue_status('test_queue' ); 274 | ~~~ 275 | 276 | #4.内存锁消息函数: 277 | ##1 LOCK操作 278 | ### lock_lock 279 | 280 | ##### *Paramer* 281 | 282 | *name*: string. 283 | 284 | ##### *Return value* 285 | 286 | *BOOL*: `TRUE` on success, `FALSE` on error. 287 | 288 | ##### *Example* 289 | 290 | ~~~ 291 | $togo->lock_lock('test_lock' ); 292 | ~~~ 293 | 294 | ##2 UNLOCK操作 295 | ### lock_unlock 296 | 297 | ##### *Paramer* 298 | 299 | *name*: string. 300 | 301 | ##### *Return value* 302 | 303 | *BOOL*: `TRUE` on success, `FALSE` on error. 304 | 305 | ##### *Example* 306 | 307 | ~~~ 308 | $togo->lock_unlock('test_lock' ); 309 | ~~~ 310 | 311 | ##3 获取一把锁的状态 312 | ### lock_status 313 | 314 | ##### *Paramer* 315 | 316 | *name*: string. 317 | 318 | ##### *Return value* 319 | 320 | *BOOL*: `STRING` on success, `FALSE` on error. 321 | 322 | ##### *Example* 323 | 324 | ~~~ 325 | $togo->lock_status('test_lock' ); 326 | ~~~ 327 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | dnl $Id$ 2 | dnl config.m4 for extension togo 3 | 4 | dnl Comments in this file start with the string 'dnl'. 5 | dnl Remove where necessary. This file will not work 6 | dnl without editing. 7 | 8 | dnl If your extension references something external, use with: 9 | 10 | dnl PHP_ARG_WITH(togo, for togo support, 11 | dnl Make sure that the comment is aligned: 12 | dnl [ --with-togo Include togo support]) 13 | 14 | dnl Otherwise use enable: 15 | 16 | PHP_ARG_ENABLE(togo, whether to enable togo support, 17 | Make sure that the comment is aligned: 18 | [ --enable-togo Enable togo support]) 19 | 20 | if test "$PHP_TOGO" != "no"; then 21 | dnl Write more examples of tests here... 22 | 23 | dnl # --with-togo -> check with-path 24 | dnl SEARCH_PATH="/usr/local /usr" # you might want to change this 25 | dnl SEARCH_FOR="/include/togo.h" # you most likely want to change this 26 | dnl if test -r $PHP_TOGO/$SEARCH_FOR; then # path given as parameter 27 | dnl TOGO_DIR=$PHP_TOGO 28 | dnl else # search default path list 29 | dnl AC_MSG_CHECKING([for togo files in default path]) 30 | dnl for i in $SEARCH_PATH ; do 31 | dnl if test -r $i/$SEARCH_FOR; then 32 | dnl TOGO_DIR=$i 33 | dnl AC_MSG_RESULT(found in $i) 34 | dnl fi 35 | dnl done 36 | dnl fi 37 | dnl 38 | dnl if test -z "$TOGO_DIR"; then 39 | dnl AC_MSG_RESULT([not found]) 40 | dnl AC_MSG_ERROR([Please reinstall the togo distribution]) 41 | dnl fi 42 | 43 | dnl # --with-togo -> add include path 44 | dnl PHP_ADD_INCLUDE($TOGO_DIR/include) 45 | 46 | dnl # --with-togo -> check for lib and symbol presence 47 | dnl LIBNAME=togo # you may want to change this 48 | dnl LIBSYMBOL=togo # you most likely want to change this 49 | 50 | dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, 51 | dnl [ 52 | dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $TOGO_DIR/lib, TOGO_SHARED_LIBADD) 53 | dnl AC_DEFINE(HAVE_TOGOLIB,1,[ ]) 54 | dnl ],[ 55 | dnl AC_MSG_ERROR([wrong togo lib version or lib not found]) 56 | dnl ],[ 57 | dnl -L$TOGO_DIR/lib -lm 58 | dnl ]) 59 | dnl 60 | dnl PHP_SUBST(TOGO_SHARED_LIBADD) 61 | 62 | PHP_NEW_EXTENSION(togo, togo.c, $ext_shared) 63 | fi 64 | -------------------------------------------------------------------------------- /config.w32: -------------------------------------------------------------------------------- 1 | // $Id$ 2 | // vim:ft=javascript 3 | 4 | // If your extension references something external, use ARG_WITH 5 | // ARG_WITH("togo", "for togo support", "no"); 6 | 7 | // Otherwise, use ARG_ENABLE 8 | // ARG_ENABLE("togo", "enable togo support", "no"); 9 | 10 | if (PHP_TOGO != "no") { 11 | EXTENSION("togo", "togo.c"); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /php_togo.h: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 5 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2016 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Author: | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | /* $Id$ */ 20 | 21 | #ifndef PHP_TOGO_H 22 | #define PHP_TOGO_H 23 | 24 | extern zend_module_entry togo_module_entry; 25 | #define phpext_togo_ptr &togo_module_entry 26 | 27 | #define PHP_TOGO_VERSION "1.0.1" 28 | #define PHP_TOGO_AUTHOR "Michael Lee" 29 | #ifdef PHP_WIN32 30 | # define PHP_TOGO_API __declspec(dllexport) 31 | #elif defined(__GNUC__) && __GNUC__ >= 4 32 | # define PHP_TOGO_API __attribute__ ((visibility("default"))) 33 | #else 34 | # define PHP_TOGO_API 35 | #endif 36 | 37 | #ifdef ZTS 38 | #include "TSRM.h" 39 | #endif 40 | 41 | #define CMD_END_SYMBOL "\r\n" 42 | #define RESPONSE_END_SYMBOL "\r\n" 43 | 44 | #ifndef NULL 45 | #define NULL 0 46 | #endif 47 | 48 | typedef struct togo_sock_ { 49 | php_stream *stream; 50 | char *host; 51 | unsigned short port; 52 | long timeout; 53 | int state; 54 | } TogoSock; 55 | 56 | #define TOGO_SOCK_STATE_FAILED 0 57 | #define TOGO_SOCK_STATE_DISCONNECTED 1 58 | #define TOGO_SOCK_STATE_UNKONW 2 59 | #define TOGO_SOCK_STATE_CONNECTED 3 60 | 61 | #define TOGO_SOCK_DEFAULT_HOST "127.0.0.1" 62 | #define TOGO_SOCK_DEFAULT_PORT 6377 63 | #define TOGO_SOCK_DEFAULT_TIMEOUT 5 64 | 65 | #define TOGO_EXCEPTION_CODE_PARAM 0x01001 66 | #define TOGO_EXCEPTION_CODE_TIMEOUT 0x01002 67 | #define TOGO_EXCEPTION_CODE_CONNECT_FAIL 0x01003 68 | #define TOGO_EXCEPTION_CODE_PROTOCOL_ERR 0x01004 69 | #define TOGO_EXCEPTION_CODE_CONN_ERR 0x01005 70 | 71 | #define TOGO_EXCEPTION_CODE_FAIL 0x020001 72 | #define TOGO_EXCEPTION_CODE_NULL 0x020002 73 | #define TOGO_EXCEPTION_CODE_COMMANDBIG 0x020003 74 | #define TOGO_EXCEPTION_CODE_BIG_CODE 0x020004 75 | #define TOGO_EXCEPTION_CODE_IS_EXIST 0x020005 76 | #define TOGO_EXCEPTION_CODE_NOT_EXIST 0x020006 77 | 78 | #define TOGO_EXCEPTION_MSG_PARAM "(%s)Paramers error,please check!" 79 | #define TOGO_EXCEPTION_MSG_TIMEOUT "(%s)Timeout %l value is invalid!" 80 | #define TOGO_EXCEPTION_MSG_CONNECT_FAIL "(%s)Connect %s:%ld(%ld) failed or timeout!" 81 | #define TOGO_EXCEPTION_MSG_PROTOCOL_ERR "Protocol error!" 82 | #define TOGO_EXCEPTION_MSG_CONN_ERR "(%s)Connect server error or broken!" 83 | 84 | #define TOGO_RESPONSE_MSG_OK "TOGO_OK" 85 | #define TOGO_RESPONSE_MSG_FAIL "TOGO_FAIL" 86 | #define TOGO_RESPONSE_MSG_NULL "TOGO_NULL" 87 | #define TOGO_RESPONSE_MSG_COMMANDBIG "TOGO_COMMAND_TOO_BIG" 88 | #define TOGO_RESPONSE_MSG_BIG "TOO_BIG" 89 | #define TOGO_RESPONSE_MSG_IS_EXIST "TOGO_IS_EXIST" 90 | #define TOGO_RESPONSE_MSG_NOT_EXIST "TOGO_NOT_EXIST" 91 | 92 | #define TOGO_RESPONSE_MSG_HEADER "TOGO_S" 93 | #define TOGO_RESPONSE_MSG_END "TOGO_E" 94 | #define TOGO_SOCK_NAME "Togo socket buffer" 95 | 96 | static void togo_destructor_togo_sock(zend_rsrc_list_entry * TSRMLS_DC); 97 | PHPAPI zend_class_entry *togo_get_exception_base(int TSRMLS_DC); 98 | PHPAPI void togo_sock_free(TogoSock *); 99 | PHPAPI zend_class_entry *togo_get_exception_base(int TSRMLS_CC); 100 | PHPAPI TogoSock* togo_sock_create(char *,int ,unsigned short ,long); 101 | PHPAPI int togo_sock_connect(TogoSock * TSRMLS_DC); 102 | PHPAPI int toso_sock_open_server(TogoSock *,int TSRMLS_DC); 103 | PHPAPI int togo_sock_disconnect(TogoSock * TSRMLS_DC); 104 | PHPAPI int togo_sock_write(TogoSock * ,char * ,size_t TSRMLS_DC); 105 | PHPAPI void togo_sock_eof(TogoSock * TSRMLS_DC); 106 | PHPAPI int togo_sock_get(TogoSock ** ,zval * TSRMLS_DC); 107 | PHPAPI char *togo_sock_read(TogoSock *, int * TSRMLS_DC); 108 | PHPAPI char *togo_sock_response_parser(char *inbuf TSRMLS_DC); 109 | PHPAPI int togo_sock_close(zval *); 110 | PHPAPI int togo_sock_response_state_check(char * TSRMLS_DC); 111 | 112 | PHP_METHOD(Togo ,__construct); 113 | PHP_METHOD(Togo ,__destruct); 114 | PHP_METHOD(Togo ,connect); 115 | PHP_METHOD(Togo ,write); 116 | PHP_METHOD(Togo, version); 117 | PHP_METHOD(Togo, counter_plus); 118 | PHP_METHOD(Togo, counter_reset); 119 | PHP_METHOD(Togo, counter_minus); 120 | PHP_METHOD(Togo, counter_get); 121 | PHP_METHOD(Togo, lock_lock); 122 | PHP_METHOD(Togo, lock_unlock); 123 | PHP_METHOD(Togo, lock_status); 124 | PHP_METHOD(Togo, queue_lpush); 125 | PHP_METHOD(Togo, queue_lpop); 126 | PHP_METHOD(Togo, queue_rpush); 127 | PHP_METHOD(Togo, queue_rpop); 128 | PHP_METHOD(Togo, queue_count); 129 | PHP_METHOD(Togo, queue_status); 130 | PHP_METHOD(Togo, close); 131 | 132 | #ifdef ZTS 133 | #define TOGO_G(v) TSRMG(togo_globals_id, zend_togo_globals *, v) 134 | #else 135 | #define TOGO_G(v) (togo_globals.v) 136 | #endif 137 | 138 | #endif /* PHP_TOGO_H */ 139 | 140 | 141 | /* 142 | * Local variables: 143 | * tab-width: 4 144 | * c-basic-offset: 4 145 | * End: 146 | * vim600: noet sw=4 ts=4 fdm=marker 147 | * vim<600: noet sw=4 ts=4 148 | */ 149 | -------------------------------------------------------------------------------- /tests/001.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Check for togo presence 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | 20 | --EXPECT-- 21 | togo extension is available 22 | -------------------------------------------------------------------------------- /togo.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SKPHP1989/php_togo/4984f5d9af5464d617800988ac0b491f620b50f5/togo.c -------------------------------------------------------------------------------- /togo.php: -------------------------------------------------------------------------------- 1 | "; 3 | 4 | if(!extension_loaded('togo')) { 5 | dl('togo.' . PHP_SHLIB_SUFFIX); 6 | } 7 | $module = 'togo'; 8 | $functions = get_extension_funcs($module); 9 | echo "Functions available in the test extension:$br\n"; 10 | foreach($functions as $func) { 11 | echo $func."$br\n"; 12 | } 13 | echo "$br\n"; 14 | $function = 'confirm_' . $module . '_compiled'; 15 | if (extension_loaded($module)) { 16 | $str = $function($module); 17 | } else { 18 | $str = "Module $module is not compiled into PHP"; 19 | } 20 | echo "$str\n"; 21 | ?> 22 | -------------------------------------------------------------------------------- /version/php_togo_0.0.1.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SKPHP1989/php_togo/4984f5d9af5464d617800988ac0b491f620b50f5/version/php_togo_0.0.1.zip -------------------------------------------------------------------------------- /www/test.php: -------------------------------------------------------------------------------- 1 | connect('127.0.0.1' ,8787 ,1); 5 | var_dump('$togo->version()' ,$togo->version()); 6 | var_dump('$togo->counter_plus(testtt)' ,$togo->counter_plus('testtt' ,4)); 7 | var_dump('$togo->counter_get(testtt)' ,$togo->counter_get('testtt')); 8 | var_dump('$togo->counter_minus(testtt)' ,$togo->counter_minus('testtt')); 9 | var_dump('$togo->counter_get(testtt)' ,$togo->counter_get('testtt')); 10 | var_dump('$togo->lock_lock(testtt)' ,$togo->lock_lock('testtt')); 11 | var_dump('$togo->lock_unlock(testtt)' ,$togo->lock_unlock('testtt')); 12 | var_dump('$togo->lock_status(testtt)' ,$togo->lock_status('testtt')); 13 | var_dump('$togo->queue_lpush(queue)' ,$togo->queue_lpush('queue' ,1)); 14 | var_dump('$togo->queue_lpush(queue)' ,$togo->queue_lpush('queue' ,2)); 15 | var_dump('$togo->queue_lpop(queue)' ,$togo->queue_lpop('queue')); 16 | var_dump('$togo->queue_lpop(queue)' ,$togo->queue_lpop('queue')); 17 | var_dump('$togo->queue_lpop(queuel)' ,$togo->queue_rpush('queuel' ,1)); 18 | var_dump('$togo->queue_lpop(queuel)' ,$togo->queue_rpush('queuel' ,2)); 19 | var_dump('$togo->queue_count(queuel)' ,$togo->queue_count('queuel')); 20 | var_dump('$togo->queue_status(queuel)' ,$togo->queue_status('queuel')); 21 | var_dump('$togo->close()' ,$togo->close('testtt')); 22 | }catch (TogoException $e){ 23 | var_dump($e); 24 | } --------------------------------------------------------------------------------