@@ -58,9 +58,9 @@ export interface IStorage {
5858 checkUserExist ( userName : string ) : Promise < boolean > ;
5959
6060 // 搜索历史相关
61- getSearchHistory ( ) : Promise < string [ ] > ;
62- addSearchHistory ( keyword : string ) : Promise < void > ;
63- deleteSearchHistory ( keyword ?: string ) : Promise < void > ;
61+ getSearchHistory ( userName : string ) : Promise < string [ ] > ;
62+ addSearchHistory ( userName : string , keyword : string ) : Promise < void > ;
63+ deleteSearchHistory ( userName : string , keyword ?: string ) : Promise < void > ;
6464
6565 // 用户列表
6666 getAllUsers ( ) : Promise < string [ ] > ;
@@ -184,26 +184,30 @@ class RedisStorage implements IStorage {
184184 }
185185
186186 // ---------- 搜索历史 ----------
187- private shKey = 'moontv:search_history' ;
187+ private shKey ( user : string ) {
188+ return `u:${ user } :sh` ; // u:username:sh
189+ }
188190
189- async getSearchHistory ( ) : Promise < string [ ] > {
190- return ( await this . client . lRange ( this . shKey , 0 , - 1 ) ) as string [ ] ;
191+ async getSearchHistory ( userName : string ) : Promise < string [ ] > {
192+ return ( await this . client . lRange ( this . shKey ( userName ) , 0 , - 1 ) ) as string [ ] ;
191193 }
192194
193- async addSearchHistory ( keyword : string ) : Promise < void > {
195+ async addSearchHistory ( userName : string , keyword : string ) : Promise < void > {
196+ const key = this . shKey ( userName ) ;
194197 // 先去重
195- await this . client . lRem ( this . shKey , 0 , keyword ) ;
198+ await this . client . lRem ( key , 0 , keyword ) ;
196199 // 插入到最前
197- await this . client . lPush ( this . shKey , keyword ) ;
200+ await this . client . lPush ( key , keyword ) ;
198201 // 限制最大长度
199- await this . client . lTrim ( this . shKey , 0 , SEARCH_HISTORY_LIMIT - 1 ) ;
202+ await this . client . lTrim ( key , 0 , SEARCH_HISTORY_LIMIT - 1 ) ;
200203 }
201204
202- async deleteSearchHistory ( keyword ?: string ) : Promise < void > {
205+ async deleteSearchHistory ( userName : string , keyword ?: string ) : Promise < void > {
206+ const key = this . shKey ( userName ) ;
203207 if ( keyword ) {
204- await this . client . lRem ( this . shKey , 0 , keyword ) ;
208+ await this . client . lRem ( key , 0 , keyword ) ;
205209 } else {
206- await this . client . del ( this . shKey ) ;
210+ await this . client . del ( key ) ;
207211 }
208212 }
209213
@@ -371,16 +375,16 @@ export class DbManager {
371375 }
372376
373377 // ---------- 搜索历史 ----------
374- async getSearchHistory ( ) : Promise < string [ ] > {
375- return this . storage . getSearchHistory ( ) ;
378+ async getSearchHistory ( userName : string ) : Promise < string [ ] > {
379+ return this . storage . getSearchHistory ( userName ) ;
376380 }
377381
378- async addSearchHistory ( keyword : string ) : Promise < void > {
379- await this . storage . addSearchHistory ( keyword ) ;
382+ async addSearchHistory ( userName : string , keyword : string ) : Promise < void > {
383+ await this . storage . addSearchHistory ( userName , keyword ) ;
380384 }
381385
382- async deleteSearchHistory ( keyword ?: string ) : Promise < void > {
383- await this . storage . deleteSearchHistory ( keyword ) ;
386+ async deleteSearchHistory ( userName : string , keyword ?: string ) : Promise < void > {
387+ await this . storage . deleteSearchHistory ( userName , keyword ) ;
384388 }
385389
386390 // 获取全部用户名
0 commit comments