栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

bitmap过滤海量重复数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

bitmap过滤海量重复数据

需求

从海量的帖子中推荐一页帖子在首页,尽量避免用户刷到相同的帖子;

需求分析

1.针对用户推荐帖子
2.避免重复数据
3.帖子数据量巨大

实现

1.考虑用redis 布隆过滤器,但是因为我们可以得到帖子id是唯一的,直接用id作为hash值写入redis的bitmap ;
我这边是用户只要两个月不需要看到重复的数据即可;如果是没有时间限制则去掉对应的时间限制

RedisAbstract 缓存抽象类

abstract class RedisAbstract
{
    protected $prefix = 'rds';

    protected $name = 'default';

    
    protected function redis()
    {
        return Yii::$app->redisData;
    }

    
    protected function getCacheKey($key = '')
    {
        $params = [$this->prefix, $this->name];
        if (is_array($key)) {
            $params = array_merge($params, $key);
        } else {
            $params[] = $key;
        }

        return $this->filter($params);
    }

    
    protected function filter(array $params = [])
    {
        foreach ($params as $k => $param) {
            $params[$k] = trim($param, ':');
        }

        return implode(':', array_filter($params));
    }

}

FilterRepeated 过滤器

class FilterRepeated extends RedisAbstract
{
    protected $month;
    protected $userId;
    protected $name = "post";

    protected $bucket;
    protected $prefix = 'bl';

    protected function __construct($userId, $month)
    {
        $this->userId = $userId;
        $this->month = $month;
        $keyArr = [$userId, $this->month];
        $this->bucket = $this->getCacheKey($keyArr);
    }
    
    
    public static function fromThisMonth($userId)
    {
        return new self($userId, date('y_m'));
    }

    
    public static function fromLastMonth($userId)
    {
        return new self($userId, date('y_m', strtotime('-1 month')));
    }

    
    public static function existsLastTwoMonth($userId, $string)
    {
        $flag = false;
        //当月是否有看过
        $thisMonth = self::fromThisMonth($userId);
        if ($thisMonth->exists($string)) {
            $flag = true;
        }
        //没看过的话,上月是否看过
        if (!$flag) {
            $lastMonthFilter = self::fromLastMonth($userId);
            if ($lastMonthFilter->exists($string)) {
                $flag = true;
            }
        }

        return $flag;
    }

    
    public function add(int $id)
    {
        return $this->redis()->setbit($this->bucket, $id, 1);
    }

    
    public function addMulti(array $ids)
    {
        if (count($ids) > 0) {
            $this->redis()->multi();
            foreach ($ids as $id) {
                $this->redis()->setbit($this->bucket, $id, 1);
            }
            return $this->redis()->exec();
        }
    }

    
    public function exists($id)
    {
        $bit = $this->redis()->getbit($this->bucket, $id);
        if ($bit == 0) {
            return false;
        }
        return true;
    }

    
    public function existMulti($ids)
    {
        $this->redis()->multi();
        foreach ($ids as $id) {
            $this->redis()->getbit($this->bucket, $id);
        }
        return  $this->redis()->exec();
    }

    
    public function flush()
    {
        $this->redis()->expire($this->bucket, 0);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号