A4 - 边缘功能的抽离以及缓存预热方案

A4 - 边缘功能的抽离以及缓存预热方案

黄鹏宇 472 2022-10-25

一、边缘功能的抽离

0. 好处与坏处

好处

坏处

1. 哪些功能适合抽离

  • 消息提醒
  • 单词搜索
  • 句子相关
  • 定时任务
  • 支付功能
  • 缓存
  • 单词本

2. 什么层级的抽离

持久层独立

3. 系统通信方案

  • 如何获取可用的远程服务器
    服务注册与发现
  • 如何表示数据
    序列化与反序列化
  • 如何传递数据
    网络通讯
  • 服务端如何确定并调用目标方法
    调用方法映射

二、缓存预热方案

哪些是热点缓存

1. 卡片上的单词 CACHE_WORD_BY_CARD_ID

哪些卡片属于热点卡片? next_practice_time > 现在的,且创建时间在一个月内的,数量为2万
SELECT count(*) FROM `card` WHERE next_need_review_time > now() AND DATEDIFF(NOW(),CREATE_DATE) <30

2. 单词信息 CACHE_SEARCH_WORD

以及我们基础词典的,数量为30000+
SELECT DISTINCT(`name`) as 'word' FROM base_word

3. 自定义释义

4. 是否收藏单词

怎么预热

不嵌在主体程序里,主程序只暴露几个接口CacheController,并鉴权

  1. 单词
import requests
import mysql.connector
from mysql.connector.pooling import MySQLConnectionPool

# 主表
baseMysqlPool = MySQLConnectionPool(
        host = DB_URL,
        user ="root",        
        port = 59958,
        passwd = DB_PASSWORD,
        database = DB_BASE_NAME,
        charset ='utf8',
        autocommit = True,
        pool_size = 2,
        auth_plugin = 'mysql_native_password'
)


def splitList2NList(originList, n):
    if len(originList) % n == 0:
        cnt = len(originList) // n
    else:
        cnt = len(originList) // n + 1
 
    for i in range(0, n):
        yield originList[i*cnt:(i+1)*cnt]



def searchWordCache(word):
	r = requests.get("URL/cache/word?word={0}&secretKey={1}".format(word,"HPyuko12!!"))
	print(r.text)

def searchWordListCache(wordList):
	wordListStr = ",".join(wordList)
	r = requests.get("URL/cache/wordlist?wordList={0}&secretKey={1}".format(wordListStr,"HPyuko12!!"))
	print(r.text)

def getAllWordFromDB():
    conn = baseMysqlPool.get_connection()
    cursor = conn.cursor()
    sql = "SELECT DISTINCT(`name`) as 'word' FROM base_word" 
    cursor.execute(sql)
    result = cursor.fetchall()
    conn.commit()
    cursor.close()
    conn.close()
    return result


def main():
	# 从数据库里拉数据
	wordListDB = getAllWordFromDB()
	wordList = []
	for word in wordListDB:
		wordList.append(word[0])


	newList = splitList2NList(wordList,100)
	for smallList in newList:
		searchWordListCache(smallList)

if __name__ == '__main__':
	main()