Table of Contents
回复内容:
数据类型选择
执行过程:
Home Backend Development PHP Tutorial 新浪微博如何获取我关注的人的微博的?

新浪微博如何获取我关注的人的微博的?

Jun 06, 2016 pm 08:50 PM
mysql php redis

比如,我本身已经关注了a,b,c

那sql可能这样的

$db->select('select * from posts where authorid in (a,b,c) order by timestamp desc limit N');
Copy after login
Copy after login

但是,假如我关注了1000个人呢?10000个呢?

使用redis如何操作呢?

记事狗微博是这么干的

SELECT tid FROM jishigou_topic   WHERE 1  AND  uid IN('1','2') AND `type` IN('first','forward','both')   AND `dateline`>1366483026    ORDER BY `dateline` DESC   LIMIT 1000   

REPLACE INTO jishigou_cache_7 SET `key`='1-topic-myhome--0',`dateline`='1369075026',`val`='YToyOntzOjQ6ImRhdGEiO2E6Mjp7czo0OiJsaXN0IjthOjE6e2k6MDthOjQ6e2k6MDtzOjE6IjQiO2k6MTtzOjE6IjMiO2k6MjtzOjE6IjIiO2k6MztzOjE6IjEiO319czo1OiJjb3VudCI7aTo0O31zOjQ6ImxpZmUiO2k6NjAwO30='

SELECT * FROM jishigou_topic WHERE `tid` IN ('4','3','2','1') 
Copy after login
Copy after login

回复内容:

比如,我本身已经关注了a,b,c

那sql可能这样的

$db->select('select * from posts where authorid in (a,b,c) order by timestamp desc limit N');
Copy after login
Copy after login

但是,假如我关注了1000个人呢?10000个呢?

使用redis如何操作呢?

记事狗微博是这么干的

SELECT tid FROM jishigou_topic   WHERE 1  AND  uid IN('1','2') AND `type` IN('first','forward','both')   AND `dateline`>1366483026    ORDER BY `dateline` DESC   LIMIT 1000   

REPLACE INTO jishigou_cache_7 SET `key`='1-topic-myhome--0',`dateline`='1369075026',`val`='YToyOntzOjQ6ImRhdGEiO2E6Mjp7czo0OiJsaXN0IjthOjE6e2k6MDthOjQ6e2k6MDtzOjE6IjQiO2k6MTtzOjE6IjMiO2k6MjtzOjE6IjIiO2k6MztzOjE6IjEiO319czo1OiJjb3VudCI7aTo0O31zOjQ6ImxpZmUiO2k6NjAwO30='

SELECT * FROM jishigou_topic WHERE `tid` IN ('4','3','2','1') 
Copy after login
Copy after login

YY一下如果是我,并且是用redis,我会如何解决这个问题。

数据类型选择

我会选择用list存储好友的微博,有两个好处:

  1. 插入很快,时间复杂度O(1)
  2. 在数据量级比较小的时候(譬如10-20条)查找也很快,时间复杂度0(N)

执行过程:

假如A有三个好友,B、C、D,然后A发了一条微博:hello, 这时候A B C D 理论上都能够马上看到这条新微博,执行过程如下:

  1. A往自己的微博列表中插入这条hello消息,lpush 或者 rpush
  2. 往B C D的微博列表中分别插入这条hello消息,lpush 或者 rpush

当用户打开或者刷新页面时,就能看到好友的新鲜事了,但这又一个问题:如果A有1w个好友,那他岂不是要往1w个好友的微博列表中插入hello消息,那页面非卡死不可! 这个问题如何解决呢?

我觉得是这样的: 首先自己发新微博自己一定要立即看到,但是其他好友可以稍微慢个几s看到也没多大关系,所以首先往自己的微博列表中插入信息是必须的,然后更新好友微博列表操作可以采用消息的方式异步化掉,也就是说新的改进过程如下:

  1. 往自己的微博列表中插入这条hello消息,lpush 或者 rpush
  2. 发送更新信息: update B C D "hello"

当某个消息接收器收到update信息时,就会去更新响应人的微博列表

以上内容,纯属YY

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

Summary of phpmyadmin vulnerabilities Summary of phpmyadmin vulnerabilities Apr 10, 2025 pm 10:24 PM

The key to PHPMyAdmin security defense strategy is: 1. Use the latest version of PHPMyAdmin and regularly update PHP and MySQL; 2. Strictly control access rights, use .htaccess or web server access control; 3. Enable strong password and two-factor authentication; 4. Back up the database regularly; 5. Carefully check the configuration files to avoid exposing sensitive information; 6. Use Web Application Firewall (WAF); 7. Carry out security audits. These measures can effectively reduce the security risks caused by PHPMyAdmin due to improper configuration, over-old version or environmental security risks, and ensure the security of the database.

See all articles