国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Message system design and implementation, message system design and implementation
Product Analysis
Brief Book
Zhihu
Three categories of messages
Language analysis of reminders
Two ways to get messages
訂閱
聚合
五個(gè)實(shí)體
行為分解
模型設(shè)計(jì)
Notify
UserNotify
Subscription
SubscriptionConfig
配置文件 NotifyConfig
服務(wù)層 NotifyService
NotifyService擁有以下方法:
各方法的處理邏輯如下:
時(shí)序圖
提醒的訂閱、創(chuàng)建、拉取
公告的創(chuàng)建、拉取
信息的創(chuàng)建
Home Backend Development PHP Tutorial Message system design and implementation, message system design and implementation_PHP tutorial

Message system design and implementation, message system design and implementation_PHP tutorial

Jul 12, 2016 am 08:55 AM

Message system design and implementation, message system design and implementation

Text/JC_Huang (author of the short book)
Original link: http://www.jianshu.com/p/f4d7827821f1
The copyright belongs to the author. Please contact the author for authorization to reprint, and mark the "author of the brief book".

Product Analysis

First, let’s take a look at how messages are implemented in the market.

Brief Book

Jianshu’s messaging system is mainly divided into two types

  • Short message
  • Reminder

Short message
The nature of a short message is actually the same as a private message. It is a message sent from a user to a user with specific information content.


Simple letters

Reminder
A reminder is a message sent by the system, its copy format is fixed, and it usually has a hyperlink to a special object.


Brief reminder

Zhihu

Zhihu is the same as Jianshu, there are two main types:

  • Private message
  • Message

Private message
Similar to Jianshu, it can be a message sent by users to users, or it can also be a message sent by administrators to users.


Zhihu private message

Messages
Zhihu’s messages are even better than Jianshu’s reminders. Zhihu will gather multiple similar messages to reduce users’ reading pressure.


Zhihu news

Three categories of messages

Through a simple analysis of the two products, we can conclude that their messages have two categories. On this basis, we add another one: announcement.
The main nature of the announcement is that the system sends a message with specific content, which can be read by all users on the site.
So, there are three categories of messages:

Language analysis of reminders

We take a set of reminder samples from the short book:

  • 3dbe1bd90774 followed you
  • magicdawn liked your article "Three ways to implement single sign-on"
  • Unscrupulous program likes your article "How to design user permission control based on RESTful API?" 》
  • alexcc4 liked your article "Implementing Unit Testing in Nodejs"
  • You wrote in "How to design user permission control based on RESTful API?" 》Received a comment from cnlinjie
  • Your article "Session Principle" has been added to the topic "ios Development"

Analyze the sentence structure and the content of the reminder is nothing more than

<p>「誰(shuí)對(duì)一樣屬于誰(shuí)的事物做了什么操作」<br />「someone do something in someone's something」</p>

someone = the triggerer of the reminder, or the sender, marked as sender
do something = the action of the reminder, comments, likes, and attention all belong to one action, marked as action
something = the action object of the reminder , this is specific to which article it is, marked as target
someone's = the owner of the reminder action object, marked as targetOwner

This is clear. The sender and targetOwner are the users of the website, and the target is the specific article. If the reminder object is not limited to the article, but there are others, you need to add a targetType to mark it. Is the goal an article or something else? The action is fixed. There may be only a few actions that will trigger reminders on the entire website: comment, like, follow... (or other actions that the business needs to remind)

Two ways to get messages

  • Push
  • Pull

Take Zhihu as an example
Pushing is more common. It is necessary to maintain a list of followers for a certain question. Whenever the conditions for pushing this question are triggered (for example, someone answers the question ), send this notification to each follower.

Pulling is a bit more troublesome, which is the reverse of pushing. For example, each user has a list of concerns. Whenever the user goes online, each issue is polled. When the event list of the issue appears, I originally pulled the information with a large timestamp.

而我們則根據(jù)消息的不同分類采用不同的獲取方式
通告和提醒,適合使用拉取的方式,消息產(chǎn)生之后,會(huì)存在消息表中,用戶在某一特定的時(shí)間根據(jù)自己關(guān)注問(wèn)題的表進(jìn)行消息的拉取,然后添加到自己的消息隊(duì)列中,

信息,適合使用推的方式,在發(fā)送者建立一條信息之后,同時(shí)指定接收者,把消息添加到接收者的消息隊(duì)列中。

訂閱

根據(jù)提醒使用拉取的方式,需要維護(hù)一個(gè)關(guān)注某一事物的列表。
這種行為,我們稱之為:「訂閱」Subscribe

一則訂閱有以下三個(gè)核心屬性

  • 訂閱的目標(biāo) target
  • 訂閱的目標(biāo)類型 targetType
  • 訂閱的動(dòng)作 action

比如我發(fā)布了一篇文章,那么我會(huì)訂閱文章《XXX》的評(píng)論動(dòng)作,所以文章《XXX》每被人評(píng)論了,就需要發(fā)送一則提醒告知我。

訂閱的規(guī)則還可以擴(kuò)展
我喜歡了一篇文章,和我發(fā)布了一篇文章,訂閱的動(dòng)作可能不一樣。
喜歡了一篇文章,我希望我訂閱這篇文章更新、評(píng)論的動(dòng)作。
而發(fā)布了一篇文章,我希望我只是訂閱這篇文章的評(píng)論動(dòng)作。

這時(shí)候就需要多一個(gè)參數(shù):subscribReason
不同的subscribReason,對(duì)應(yīng)著一個(gè)動(dòng)作數(shù)組,
subscribReason = 喜歡,對(duì)應(yīng)著 actions = [更新,評(píng)論]
subscribReason = 發(fā)布,對(duì)應(yīng)著 actions = [評(píng)論]

訂閱的規(guī)則還還可以擴(kuò)展
用戶可能會(huì)有一個(gè)自己的訂閱設(shè)置,比如對(duì)于所有的喜歡的動(dòng)作,我都不希望接收。
比如Knewone的提醒設(shè)置


Knewone提醒設(shè)置

所以我們需要再維護(hù)一個(gè)表:SubscriptionConfig,來(lái)存放用戶的提醒設(shè)置。
并且,當(dāng)用戶沒(méi)有提醒設(shè)置的時(shí)候,可以使用系統(tǒng)提供的一套默認(rèn)設(shè)置:defaultSubscriptionConfig

聚合

如果我發(fā)布了一篇文章《XXX》,在我不在線的時(shí)候,被評(píng)論了10遍,當(dāng)我一上線的時(shí)候,應(yīng)該是收到十條信息類似于:「誰(shuí)誰(shuí)誰(shuí)評(píng)論了你的文章《XXX》」?
還是應(yīng)該收到一條信息:「甲、乙、丙、丁...評(píng)論了你的文章《XXX》」?

知乎在聚合上做的很優(yōu)秀,要知道他們要實(shí)現(xiàn)這個(gè)還是挺有技術(shù)的:
知乎的消息機(jī)制,在技術(shù)上如何設(shè)計(jì)與規(guī)劃?
網(wǎng)站的消息(通知)系統(tǒng)一般是如何實(shí)現(xiàn)的?

關(guān)于這部分功能,我們還沒(méi)有具體的實(shí)現(xiàn)方法,暫時(shí)也無(wú)法講得更加詳細(xì)。⊙﹏⊙

五個(gè)實(shí)體

通過(guò)上面的分析,大概知道做這個(gè)消息系統(tǒng),需要哪些實(shí)體類:

行為分解

說(shuō)了這么多,整理一下整個(gè)消息流程的一些行為:

  • 系統(tǒng)或者管理員,創(chuàng)建消息
    • createNotify (make announce | remind | message)
  • 用戶,訂閱消息,取消訂閱
    • subscribe, cancelSubscription
  • 用戶管理訂閱設(shè)置
    • getSubscriptionConfig, updateSubscriptionConfig
  • 用戶,拉取消息
    • pullNotify (pull announce | remind | message | all)
  • 用戶,查詢消息隊(duì)列
    • getUserNotify(get announce | remind | message | all)
  • 用戶閱讀消息
    • read
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

模型設(shè)計(jì)

Notify

<code class="javascript">id            : {type: <span class="hljs-string">'integer', primaryKey: <span class="hljs-literal">true},        <span class="hljs-comment">// 主鍵
content     : {type: <span class="hljs-string">'text'},    <span class="hljs-comment">// 消息的內(nèi)容
type        : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true, enum: [<span class="hljs-number">1, <span class="hljs-number">2, <span class="hljs-number">3]},  <span class="hljs-comment">// 消息的類型,1: 公告 Announce,2: 提醒 Remind,3:信息 Message
target      : {type: <span class="hljs-string">'integer'},    <span class="hljs-comment">// 目標(biāo)的ID
targetType  : {type: <span class="hljs-string">'string'},    <span class="hljs-comment">// 目標(biāo)的類型
action      : {type: <span class="hljs-string">'string'},    <span class="hljs-comment">// 提醒信息的動(dòng)作類型
sender      : {type: <span class="hljs-string">'integer'},    <span class="hljs-comment">// 發(fā)送者的ID
createdAt    : {type: <span class="hljs-string">'datetime', required: <span class="hljs-literal">true}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

Save Remind
消息表,我們需要target、targetType字段,來(lái)記錄該條提醒所關(guān)聯(lián)的對(duì)象。而action字段,則記錄該條提醒所關(guān)聯(lián)的動(dòng)作。
比如消息:「小明喜歡了文章」
則:

<code class="javascript">target = <span class="hljs-number">123,  <span class="hljs-comment">// 文章ID
targetType = <span class="hljs-string">'post',  <span class="hljs-comment">// 指明target所屬類型是文章
sender = <span class="hljs-number">123456  <span class="hljs-comment">// 小明ID</span></span></span></span></span></span></code>

Save Announce and Message
當(dāng)然,Notify還支持存儲(chǔ)公告和信息。它們會(huì)用到content字段,而不會(huì)用到target、targetTypeaction字段。

UserNotify

<code class="javascript">id            : {type: <span class="hljs-string">'integer', primaryKey: <span class="hljs-literal">true},        <span class="hljs-comment">// 主鍵
isRead      : {type: <span class="hljs-string">'boolean', required: <span class="hljs-literal">true},   
user        : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true},  <span class="hljs-comment">// 用戶消息所屬者
notify      : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true}   <span class="hljs-comment">// 關(guān)聯(lián)的Notify
createdAt    : {type: <span class="hljs-string">'datetime', required: <span class="hljs-literal">true}</span></span></span></span></span></span></span></span></span></span></span></span></span></code>

我們用UserNotify來(lái)存儲(chǔ)用戶的消息隊(duì)列,它關(guān)聯(lián)一則提醒(Notify)的具體內(nèi)容。
UserNotify的創(chuàng)建,主要通過(guò)兩個(gè)途徑:

Subscription

<code class="javascript">target      : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true},    <span class="hljs-comment">// 目標(biāo)的ID
targetType  : {type: <span class="hljs-string">'string', required: <span class="hljs-literal">true},    <span class="hljs-comment">// 目標(biāo)的類型
action      : {type: <span class="hljs-string">'string'},   <span class="hljs-comment">// 訂閱動(dòng)作,如: comment/like/post/update etc.
user        : {type: <span class="hljs-string">'integer'},
createdAt    : {type: <span class="hljs-string">'datetime', required: <span class="hljs-literal">true}</span></span></span></span></span></span></span></span></span></span></span></code>

訂閱,是從Notify表拉取消息到UserNotify的前提,用戶首先訂閱了某一個(gè)目標(biāo)的某一個(gè)動(dòng)作,在此之后產(chǎn)生這個(gè)目標(biāo)的這個(gè)動(dòng)作的消息,才會(huì)被通知到該用戶。
如:「小明關(guān)注了產(chǎn)品A的評(píng)論」,數(shù)據(jù)表現(xiàn)為:

<code class="javascript">target: <span class="hljs-number">123,  <span class="hljs-comment">// 產(chǎn)品A的ID
targetType: <span class="hljs-string">'product',
action: <span class="hljs-string">'comment',
user: <span class="hljs-number">123  <span class="hljs-comment">// 小明的ID</span></span></span></span></span></span></code>

這樣,產(chǎn)品A下產(chǎn)生的每一條評(píng)論,都會(huì)產(chǎn)生通知給小明了。

SubscriptionConfig

<code class="javascript">action: {type: <span class="hljs-string">'json', required: <span class="hljs-literal">true},   <span class="hljs-comment">// 用戶的設(shè)置
user: {type: <span class="hljs-string">'integer'}</span></span></span></span></code>

不同用戶可能會(huì)有不一樣的訂閱習(xí)慣,在這個(gè)表中,用戶可以統(tǒng)一針對(duì)某種動(dòng)作進(jìn)行是否訂閱的設(shè)置。而默認(rèn)是使用系統(tǒng)提供的默認(rèn)配置:

<code class="javascript">defaultSubscriptionConfig: {
  <span class="hljs-string">'comment'   : <span class="hljs-literal">true,    <span class="hljs-comment">// 評(píng)論
  <span class="hljs-string">'like'      : <span class="hljs-literal">true,    <span class="hljs-comment">// 喜歡
}</span></span></span></span></span></span></code>
<p>在這套模型中,<code>targetType</code>、<code>action</code>是可以根據(jù)需求來(lái)擴(kuò)展的,例如我們還可以增加多幾個(gè)動(dòng)作的提醒:<code>hate</code>被踩、<code>update</code>被更新....諸如此類。</p>

配置文件 NotifyConfig

<code class="javascript"><span class="hljs-comment">// 提醒關(guān)聯(lián)的目標(biāo)類型
targetType: {
  PRODUCT : <span class="hljs-string">'product',    <span class="hljs-comment">// 產(chǎn)品
  POST    : <span class="hljs-string">'post'    <span class="hljs-comment">// 文章
},

<span class="hljs-comment">// 提醒關(guān)聯(lián)的動(dòng)作
action: {
  COMMENT   : <span class="hljs-string">'comment',  <span class="hljs-comment">// 評(píng)論
  LIKE      : <span class="hljs-string">'like',     <span class="hljs-comment">// 喜歡
},

<span class="hljs-comment">// 訂閱原因?qū)?yīng)訂閱事件
reasonAction: {
  <span class="hljs-string">'create_product'  : [<span class="hljs-string">'comment', <span class="hljs-string">'like']
  <span class="hljs-string">'like_product'    : [<span class="hljs-string">'comment'],
  <span class="hljs-string">'like_post'       : [<span class="hljs-string">'comment'],
},

<span class="hljs-comment">// 默認(rèn)訂閱配置
defaultSubscriptionConfig: {
  <span class="hljs-string">'comment'   : <span class="hljs-literal">true,    <span class="hljs-comment">// 評(píng)論
  <span class="hljs-string">'like'      : <span class="hljs-literal">true,    <span class="hljs-comment">// 喜歡
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

服務(wù)層 NotifyService

NotifyService擁有以下方法:

  • createAnnounce(content, sender)
  • createRemind(target, targetType, action, sender, content)
  • createMessage(content, sender, receiver)
  • pullAnnounce(user)
  • pullRemind(user)
  • subscribe(user, target, targetType, reason)
  • cancelSubscription(user, target ,targetType)
  • getSubscriptionConfig(userID)
  • updateSubscriptionConfig(userID)
  • getUserNotify(userID)
  • read(user, notifyIDs)

各方法的處理邏輯如下:

createAnnounce(content, sender)

createRemind(target, targetType, action, sender, content)

createMessage(content, sender, receiver)

pullAnnounce(user)

pullRemind(user)

subscribe(user, target, targetType, reason)

cancelSubscription(user, target ,targetType)

getSubscriptionConfig(userID)

updateSubscriptionConfig(userID)

getUserNotify(userID)

read(user, notifyIDs)

時(shí)序圖

提醒的訂閱、創(chuàng)建、拉取


提醒的訂閱、創(chuàng)建、拉取


我們可以在產(chǎn)品創(chuàng)建之后,調(diào)用NotifyService.subscribe方法,
然后在產(chǎn)品被評(píng)論之后調(diào)用NotifyService.createRemind方法,
再就是用戶登錄系統(tǒng)或者其他的某一個(gè)時(shí)刻調(diào)用NotifyService.pullRemind方法,
最后在用戶查詢消息隊(duì)列的時(shí)候調(diào)用NotifyService.getUserNotify方法。

公告的創(chuàng)建、拉取


公告的創(chuàng)建、拉取


在管理員發(fā)送了一則公告的時(shí)候,調(diào)用NotifyService.createAnnounce方法,
然后在用戶登錄系統(tǒng)或者其他的某一個(gè)時(shí)刻調(diào)用NotifyService.pullAnnounce方法,
最后在用戶查詢消息隊(duì)列的時(shí)候調(diào)用NotifyService.getUserNotify方法。

信息的創(chuàng)建


信息的創(chuàng)建


信息的創(chuàng)建,只需要直接調(diào)用NotifyService.createMessage方法就可以了,
在下一次用戶查詢消息隊(duì)列的時(shí)候,就會(huì)查詢這條信息。


?

? ?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1114983.htmlTechArticleMessage system design and implementation, message system design and implementation article/JC_Huang (author of the short book) Original link: http:/ /www.jianshu.com/p/f4d7827821f1 The copyright belongs to the author, please contact...
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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What are some best practices for versioning a PHP-based API? What are some best practices for versioning a PHP-based API? Jun 14, 2025 am 12:27 AM

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

What are the differences between procedural and object-oriented programming paradigms in PHP? What are the differences between procedural and object-oriented programming paradigms in PHP? Jun 14, 2025 am 12:25 AM

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

What are weak references (WeakMap) in PHP, and when might they be useful? What are weak references (WeakMap) in PHP, and when might they be useful? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

See all articles