zoukankan      html  css  js  c++  java
  • php 远程调用redis

    <?php
    $redis_conf = array (
    "active_code"=>array(
    "host" => "14.29.64.112",
    "port" => "8899",
    "pass" => "funova2014"
    )

    );

    #微信签名
    private function checkSignature()
    {
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];

    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );

    if( $tmpStr == $signature ){
    return true;
    }else{
    return false;
    }
    }
    #连接redis
    function getSocket(){
    include_once ("phpRedis.class.php");
    $phpRedis = new phpRedis ();
    $phpRedis->master = "active_code";
    include ("redis.conf.php");
    #授权
    $phpRedis->auth ($redis_conf ["active_code"]['pass']);
    return $phpRedis;
    }
    #获取激活码
    function index($key){
    #连接redis
    $redis=$this->getSocket();
    $redis->connect();
    return $redis->lpop("$key");
    #return $redis->hgetall('code_hash');
    }
    #把已经读取的激活码写入redis中
    function set_code($code,$userid){
    #连接redis
    $redis=$this->getSocket();
    $redis->connect();
    return $redis->hset('code_hash_wx',$code,$userid);
    #return $redis->hgetall('code_hash');
    }

    <?php
    #class Php5RedisException extends Exception {}
    class phpRedis
    {
    /*
    * $port 端口 6379
    * $host 主机 192.168.1.28
    */
    // redisS务器的地址

    private $md5key = "funova_aws_redis";

    public $master="active_code";

    static private $_sock = NULL;
    // 保存数据库连接资源
    public static $linkID = array ();
    /**
    * 链接redis服务器
    */
    public function connect() {
    error_reporting(E_ALL ^ E_NOTICE);

    include("redis.conf.php");
    $this->host = $redis_conf [$this->master]['host'];
    $this->port = $redis_conf [$this->master]['port'];

    if (!is_null(self::$linkID[$this->master]))
    return;
    $sock = fsockopen ( $this->host, $this->port, $errno, $errstr );

    if ($sock) {
    self::$linkID [$this->master] = $sock;
    $this->debug('Connected');
    return;
    }
    $msg = "Cannot open socket to {$this->host}:{$this->port}";
    if ($errno || $errmsg)
    $msg .= "," . ($errno ? " error $errno" : "") . ($errmsg ? " $errmsg" : "");
    error_log ( "$msg." ,3,"/tmp/hivePhpLog/redis.log");

    }
    /**
    * 错误提示
    * @param unknown_type $msg
    */
    private function debug($msg){
    //echo sprintf("[php_redis] %s ", $msg);
    }
    /**
    * 对cmd接收到的函数进行处理
    * 并且返回数据
    */
    private function cmdResponse() {
    // Read the response
    $s = trim ( $this->read () );
    switch ($s [0]) {
    case '-' : // Error message
    throw new Php5RedisException ( substr ( $s, 1 ) );
    break;
    case '+' : // Single line response
    return substr ( $s, 1 );
    case ':' : //Integer number
    return substr ( $s, 1 ) + 0;
    case '$' : //Bulk data response
    $i = ( int ) (substr ( $s, 1 ));
    if ($i == - 1)
    return null;
    $buffer = '';
    if ($i == 0){
    $s = $this->read ();
    }
    while ( $i > 0 ) {
    $s = $this->read ();
    $l = strlen ( $s );
    $i -= $l;
    if ($i < 0)
    $s = substr ( $s, 0, $i );
    $buffer .= $s;
    }
    return $buffer;
    break;
    case '*' : // Multi-bulk data (a list of values)
    $i = ( int ) (substr ( $s, 1 ));
    if ($i == - 1)
    return null;
    $res = array ();
    for($c = 0; $c < $i; $c ++) {
    $res [] = $this->cmdResponse ();
    }
    return $res;
    break;
    default :
    error_log ( 'Unknown responce line: ' . $s,3,"redis.log");
    break;
    }
    }
    /**
    * 接收命令并且返回结果集
    * @param unknown_type $command
    */
    private function cmd($command) {
    $this->debug('Command: '.$command);
    $this->connect ();
    $s = $command . " ";
    while ( $s ) {
    $i = fwrite ( self::$linkID [$this->master], $s );
    if ($i == 0)
    break;
    $s = substr ( $s, $i );
    }
    return $this->cmdResponse ();
    }
    /**
    * 读取指定长度的字符
    * @param unknown_type $len
    */
    private function read($len = 1024) {
    if ($s = fgets ( self::$linkID [$this->master] )) {
    $this->debug('Read: '.$s.' ('.strlen($s).' bytes)');
    return $s;
    }
    $this->disconnect ();
    error_log ( "Cannot read from socket.",3,"redis.log" );
    }
    /**
    * 关闭redis链接
    */
    private function disconnect() {
    if (self::$linkID [$this->master])
    @fclose ( self::$linkID [$this->master] );
    self::$linkID [$this->master] = null;
    }
    /**
    * L_C_48单一数据
    * @param $key
    * @param $value
    */
    function set($key, $value) {
    return $this->cmd ("SET $key $value");
    }
    /**
    * 批量L_C_48数据
    * @param $array
    */
    function setArray($array) {
    if(is_array($array)){
    foreach($array as $key =>$val){
    $this->cmd ("SET $key $val");
    }
    }
    return;
    }
    /**
    * 根据$key获取数据
    * @param $key
    */
    function get($key) {
    return $this->cmd ("GET $key");
    }
    /**
    * 返回匹配指定模式的所有key
    * @param $pattern
    */
    function keys($pattern) {
    return $this->cmd ("KEYS $pattern");
    }
    /**
    * 设定L_C_181
    */
    function setExpire($key,$expire){
    return $this->cmd ("expire $key $expire ");
    }
    /**
    * 从队到左边入队一个元素
    */
    function lpush($key,$value){
    if ($this->master == "debug") { // 内部
    return $this->cmd("lpush $key $value");
    } else {
    return $this->cmd("lpush $key '$value'");
    }
    }
    /**
    * 从队到左边出队一个元素
    */
    function lpop($key){
    return $this->cmd("lpop $key");
    }
    /**
    * 验证密码
    */
    function auth ( $pass ){
    return $this->cmd("auth $pass");
    }
    /**
    * 发送信息
    */
    function publish($key,$content){
    /**
    * 当前是否在开发模式
    */
    if ($this->master == "debug") { // 内部
    return $this->cmd("publish $key $content");
    } else {
    return $this->cmd("publish $key '$content'");
    }
    }
    /**
    * setnx
    */
    function setnx($key,$value){
    return $this->setnx("SETNX $key $value");
    }
    /**
    * L_C_26记录
    */
    function delete($key){
    return $this->cmd("DEL $key");
    }
    /**
    * 监听频道
    */
    function subscribe($key){
    return $this->cmd("SUBSCRIBE $key");
    }
    /**
    * 读取list文件
    */
    function lrange($key){
    return $this->cmd("LRANGE $key 0 -1");
    }
    /**
    * 插入hash数组
    */
    function hset($key,$filed, $value){
    return $this->cmd("hset $key $filed $value");
    }
    /**
    * 删除list
    */
    function lrem($key,$value){
    return $this->cmd("lrem $key -2 ".$value);
    }
    /**
    *获取所有的值
    */
    function hgetall($key){
    return $this->cmd("hgetall $key");
    }
    /**
    *判断key是否存在
    */
    function exists_key($key){

    return $this->cmd("EXISTS $key");
    }
    /**
    *判断判断key中有没有当前这个域
    */
    function exists_filed($key,$field){
    return $this->cmd("hexists $key $field");
    }
    /**
    *返回并删除链表尾元素
    */
    function rpop($key){
    return $this->cmd("rpop $Key");
    }
    /**
    *
    */
    function rpush($key,$value){

    return $this->cmd("rpush $Key $value");

    }


    }
    ?>

  • 相关阅读:
    C语言:SQLITE3的学习
    C语言:json库使用学习
    C语言:XML学习
    glib实践篇:接口定义与实现
    glib实践篇:父类与子类
    python简易爬虫实现
    Esxi开虚拟机测试性能
    Hyper-V虚拟化性能测试
    配置飞儿云平台的PHP系统
    CentOS6.9安装OpenVZ
  • 原文地址:https://www.cnblogs.com/hgj123/p/4563393.html
Copyright © 2011-2022 走看看