美文网首页编程语言-PHP
使用Predis写博客类熟悉redis的String类型命令操作

使用Predis写博客类熟悉redis的String类型命令操作

作者: 月圆星繁 | 来源:发表于2021-07-10 23:04 被阅读0次

基于Predis熟悉String类型命令


<?php

include "./vendor/autoload.php";
/**
 * 基于Predis的博客类
 */
class BlogClass
{
    private $client = null;
    // 连接redis
    public function __construct() {
        $this->client = new Predis\Client([
            'scheme' => 'tcp',
            'host' => '127.0.0.1',
            'port' => 6379
        ]);
        // 进行连接异常处理

    }

    /**
     * 生成blog的id(唯一ID生成器)
     */
    private function createBlogId() {
        return $this->client->incr('publishBlog');
    }
    /**
     * 组装部分key前缀
     */
    public function headlePrefix($id, $prefix = "") {
        if(!$prefix) {
            $prefix = "article:" . $id . ":";
        }
        return $prefix;
    } 

    /**
     * 发布blog(锁)
     */
    public function publishBlog($title, $content, $author) {
        $id = $this->createBlogId();
        $pre = $this->headlePrefix($id);
        $data = [
            $pre . "title" => $title,
            $pre . "content" => $content,
            $pre . "author" => $author,
            $pre . "createTime" => date("Y-m-d H:i:s"),
            $pre . "updateTime" => "",
            $pre . "likes" => 0,
            $pre . "views" => 0,
        ];
        return $this->client->msetnx($data);
    }

    /**
     * 更新blog
     */
    public function updateBlog($id, $title, $content)
    {
        $pre = $this->headlePrefix($id);
        $data = [
            $pre . "title" => $title,
            $pre . "content" => $content,
            $pre . "updateTime" => date("Y-m-d H:i:s"),
        ];
        return $this->client->mset($data);
    }

    /**
     * 查看blog
     */
    public function seeBlogDetail($id, $userId) {
        $pre = $this->headlePrefix($id);

        if($this->client->get($pre . "title")) {
            $this->client->incrby($pre . "views", 1);
        }
    
        $key = [
            $pre . "title" ,
            $pre . "content",
            $pre . "author",
            $pre . "createTime",
            $pre . "likes",
            $pre . "views"
        ];
        $this->operationLog($id, $userId, __FUNCTION__);
        return $this->client->mget($key);
    }

    // 点赞blog
    public function likeBlog($id, $userId) {
        $pre = $this->headlePrefix($id);
        $this->operationLog($id, $userId, __FUNCTION__);
        return $this->client->incr($pre . "likes");
    }

    /**
     * 删除blog
     */
    public function deleteBlog($id) {
        $pre = $this->headlePrefix($id);
        $data = [
            $pre . "title",
            $pre . "content",
            $pre . "author",
            $pre . "createTime",
            $pre . "updateTime",
            $pre . "likes",
            $pre . "views",
        ];
        return $this->client->del($data);
    }

    /**
     * 获取blog长度
     */
    public function getBlogLength($id) {
        $pre = $this->headlePrefix($id);
        $length = $this->client->strlen($pre . "content");
        return $length;
    }   

    /**
     * 预览blog
     */
    public function previewBlog($id, $userId) {
        $pre = $this->headlePrefix($id);
        $length = $this->client->getrange($pre . "content", 0, 24);
        $this->operationLog($id, $userId, __FUNCTION__);
        return $length;
    }

    /**
     * 操作日志
     */
    public function operationLog($id, $userId, $methodName){
        $pre = $this->headlePrefix($id);
        $title = $this->client->get($pre . "title");
        $op = [
            "previewBlog" => "预览",
            "likeBlog" => "点赞",
            "seeBlogDetail" => "查看",
        ];
        // 用户对blog文章操作了什么。
        $log = "[" . date("Y-m-d H:i:s") . " user:" . $userId . "对<" . $title .
               "> 进行了" . $op[$methodName] . " ]";
        $this->client->append($pre . "log", $log);
    }
    
    /**
     * 获取用户对文章的操作记录
     */
    public function getArticleLog($id) {
        $pre = $this->headlePrefix($id);
        return $this->client->get($pre . "log");
    }
}

相关文章

  • 使用Predis写博客类熟悉redis的String类型命令操作

    基于Predis熟悉String类型命令

  • redis命令行操作

    redis命令 string类型操作 hash类型相关操作 list类型相关操作 set类型相关操作 zset类型...

  • Redis

    安装使用 服务 开启 和 关闭 key 操作 Redis 数据类型 string(字符串) 基本命令: 数据结构 ...

  • Redis使用心得记录

    0.通用命令 1.string操作命令 注redis中字符数据类型: 2.hash操作命令 3.list操作命令 ...

  • 2018-10-19

    redis数据操作1.string类型:主要存储字符串 操作 命令设置键值 set key value设置...

  • php redis 操作手册

    String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的。意思是redis...

  • redis学习系列(二)

    redis简单操作之string类型 Redis 数据类型 Redis支持五种数据类型:string(字符串),h...

  • redis命令操作

    redis命令操作 一、string类型的数据 二、list数据类型(可以结合链表来理解) 三、Hash数据结构 ...

  • redis使用相关命令

    Redis keys 命令 下表给出了与 Redis 键相关的基本命令: Redis String类型相关命令 R...

  • Redis学习笔记02——数据类型和操作命令

    本文主要介绍 Redis 常用的基本操作命令和数据类型操作命令,也就是 Redis 的基本使用。通过学习本文,应该...

网友评论

    本文标题:使用Predis写博客类熟悉redis的String类型命令操作

    本文链接:https://www.haomeiwen.com/subject/usphpltx.html