Redis 之仿微博demo

1、用戶註冊登陸php

  

    include './header.php';
    include './function.php';

    $username = p('username');
    $password = p('password');
    $password2 = p('password2');

    if ($password != $password2) {
        redirect("./login.php", "兩次密碼輸入不一致", 3);
    }

    if (!$username || !$password) {
        redirect("./login.php", "請輸入用戶名或祕密", 3);
    }

    $redis = connRedis();
    /*
        用戶表設置
        user:id:1:username
        user:id:1:password

        user:username 1
    */
    $id = $redis->incr("global:user");
    $redis->set("user:id:$id:username", $username);
    $redis->set("user:id:$id:password", $password);
    $redis->set("user:username:$username", $id);

    //維護一個最新50個用戶的表
    $redis->lpush("newuser", $id);
    $redis->ltrim("newuser", 0, 49);

    redirect("./login.php", "用戶名:$username 註冊成功", 3);
   include './header.php';
    include './function.php';

    $username = p('username');
    $password = p('password');
    if (!$username || !$password) {
        redirect("./login.php", "請輸入用戶名或祕密", 3);
    }

    $redis = connRedis();
    $id = $redis->get("user:username:$username");
    $oldPassword = $redis->get("user:id:$id:password");
    closeRedis($redis);
    if ($password != $oldPassword) {
        redirect("./login.php", "用戶名或祕密不正確", 3);
    }
    //設置cookie
    setcookie("id", $id);
    setcookie("username", $username);
    setcookie("password", $password);
    redirect("./home.php", "登陸成功", 3);

2、發表動態redis

  

    include './header.php';
    include './function.php';

    $status = p('status');
    if (empty($status)) {
        redirect("./home.php", "請輸入內容", 3);
    }
    /*
        post表(發動態表)
        post:id:1:uid 
        post:id:1:content
    */
    $redis = connRedis();
    $id = $redis->incr("global:post");

    $redis->hMset("post:id:$id", array("uid" => $_COOKIE['id'], "content" => $status, "time" => date("Y-m-d H:i:s", time()), "username" => $_COOKIE['username']));

    //最近50條發佈的信息
    $redis->lpush("newpost", $id);
    $redis->ltrim("newpost", 0, 49);

    //獲取個人粉絲,並把個人動態發給他
    $fans = $redis->smembers("flowing:userid:{$_COOKIE['id']}");
    $fans[] = $_COOKIE['id'];
    foreach ($fans as $fansid) {
        $redis->lpush("receivepost:$fansid", $id);
    }

3、關注頁cookie

    include './header.php';
    include './function.php';

    $uid = g("uid");
    $f = g("f");
    $redis = connRedis();
    $u = $redis->get("user:id:$uid:username");
    if (!$u) {
        redirect("./home.php", "非法數據", 3);
    }
    if ($f) {
        //關注
        $redis->sAdd("flow:userid:{$_COOKIE['id']}", $uid);
        $redis->sAdd("flowing:userid:$uid", $_COOKIE['id']);
        $msg = "關注成功";
    }else{
        //取消
        $redis->srem("flow:userid:{$_COOKIE['id']}", $uid);
        $redis->srem("flowing:userid:$uid", $_COOKIE['id']);
        $msg = "取消關注成功";
    }
    redirect("./profile.php?u=$u", $msg, 3);    

4、熱點頁post

  

    include_once("./header.php");
    include_once("./function.php");
    if (!$_COOKIE['id']) {
        redirect("./login.php", "請先登陸", 3);
    }
    $redis = connRedis();
    $data = $redis->sort("newuser", array("get" => "user:id:*:username", "limit" => array(0,1), "sort" => "desc"));
    //獲取最新發布的動態id
    $newpost = $redis->lRange("newpost", 0, -1);    
相關文章
相關標籤/搜索