11package  com .exadel .frs .core .trainservice .cache ;
22
3+ import  com .exadel .frs .commonservice .entity .Embedding ;
4+ import  com .exadel .frs .commonservice .projection .EmbeddingProjection ;
35import  com .exadel .frs .core .trainservice .dto .CacheActionDto ;
6+ import  com .exadel .frs .core .trainservice .dto .CacheActionDto .AddEmbeddings ;
7+ import  com .exadel .frs .core .trainservice .dto .CacheActionDto .CacheAction ;
8+ import  com .exadel .frs .core .trainservice .dto .CacheActionDto .RemoveEmbeddings ;
9+ import  com .exadel .frs .core .trainservice .dto .CacheActionDto .RemoveSubjects ;
10+ import  com .exadel .frs .core .trainservice .dto .CacheActionDto .RenameSubjects ;
411import  com .exadel .frs .core .trainservice .service .EmbeddingService ;
512import  com .exadel .frs .core .trainservice .service .NotificationSenderService ;
613import  com .google .common .cache .Cache ;
714import  com .google .common .cache .CacheBuilder ;
8- import  lombok .RequiredArgsConstructor ;
9- import  lombok .extern .slf4j .Slf4j ;
10- import  org .springframework .stereotype .Component ;
11- 
15+ import  java .util .List ;
16+ import  java .util .Map ;
1217import  java .util .Optional ;
1318import  java .util .concurrent .TimeUnit ;
1419import  java .util .function .Consumer ;
20+ import  lombok .RequiredArgsConstructor ;
21+ import  lombok .extern .slf4j .Slf4j ;
22+ import  org .springframework .stereotype .Component ;
1523
1624import  static  com .exadel .frs .core .trainservice .system .global .Constants .SERVER_UUID ;
1725
@@ -34,34 +42,81 @@ public class EmbeddingCacheProvider {
3442                    .build ();
3543
3644    public  EmbeddingCollection  getOrLoad (final  String  apiKey ) {
37- 
3845        var  result  = cache .getIfPresent (apiKey );
39- 
4046        if  (result  == null ) {
4147            result  = embeddingService .doWithEnhancedEmbeddingProjectionStream (apiKey , EmbeddingCollection ::from );
42- 
4348            cache .put (apiKey , result );
44- 
45-             notifyCacheEvent ("UPDATE" , apiKey );
4649        }
47- 
4850        return  result ;
4951    }
5052
51-     public  void  ifPresent (String  apiKey , Consumer < EmbeddingCollection >  consumer ) {
53+     public  void  removeEmbedding (String  apiKey , EmbeddingProjection   embedding ) {
5254        Optional .ofNullable (cache .getIfPresent (apiKey ))
53-                 .ifPresent (consumer );
55+             .ifPresent (
56+                 ec  -> {
57+                     ec .removeEmbedding (embedding );
58+                     notifyCacheEvent (
59+                         CacheAction .REMOVE_EMBEDDINGS ,
60+                         apiKey ,
61+                         new  RemoveEmbeddings (Map .of (embedding .subjectName (), List .of (embedding .embeddingId ())))
62+                     );
63+                 }
64+             );
65+     }
5466
55-         cache .getIfPresent (apiKey );
56-         notifyCacheEvent ("UPDATE" , apiKey );
67+     public  void  updateSubjectName (String  apiKey , String  oldSubjectName , String  newSubjectName ) {
68+         Optional .ofNullable (cache .getIfPresent (apiKey ))
69+             .ifPresent (
70+                 ec  -> {
71+                     ec .updateSubjectName (oldSubjectName , newSubjectName );
72+                     notifyCacheEvent (CacheAction .RENAME_SUBJECTS , apiKey , new  RenameSubjects (Map .of (oldSubjectName , newSubjectName )));
73+                 }
74+             );
75+     }
76+ 
77+     public  void  removeBySubjectName (String  apiKey , String  subjectName ) {
78+         Optional .ofNullable (cache .getIfPresent (apiKey ))
79+             .ifPresent (
80+                 ec  -> {
81+                     ec .removeEmbeddingsBySubjectName (subjectName );
82+                     notifyCacheEvent (CacheAction .REMOVE_SUBJECTS , apiKey , new  RemoveSubjects (List .of (subjectName )));
83+                 }
84+             );
85+     }
86+ 
87+ 
88+     public  void  addEmbedding (String  apiKey , Embedding  embedding ) {
89+         Optional .ofNullable (cache .getIfPresent (apiKey ))
90+             .ifPresent (
91+                 ec  -> {
92+                     ec .addEmbedding (embedding );
93+                     notifyCacheEvent (CacheAction .ADD_EMBEDDINGS , apiKey , new  AddEmbeddings (List .of (embedding .getId ())));
94+                 }
95+             );
96+     }
97+ 
98+     /** 
99+      * Method can be used to make changes in cache without sending notification. 
100+      * Use it carefully, because changes you do will not be visible for other compreface-api instances 
101+      * 
102+      * @param apiKey domain 
103+      * @param action what to do with {@link EmbeddingCollection} 
104+      */ 
105+     public  void  expose (String  apiKey , Consumer <EmbeddingCollection > action ) {
106+         Optional .ofNullable (cache .getIfPresent (apiKey ))
107+                 .ifPresent (action );
57108    }
58109
59110    public  void  invalidate (final  String  apiKey ) {
60111        cache .invalidate (apiKey );
61-         notifyCacheEvent ("DELETE" , apiKey );
112+         notifyCacheEvent (CacheAction . INVALIDATE , apiKey ,  null );
62113    }
63114
64- 
115+     /** 
116+      * @deprecated 
117+      * See {@link com.exadel.frs.core.trainservice.service.NotificationHandler#handleUpdate(CacheActionDto)} 
118+      */ 
119+     @ Deprecated (forRemoval  = true )
65120    public  void  receivePutOnCache (String  apiKey ) {
66121        var  result  = embeddingService .doWithEnhancedEmbeddingProjectionStream (apiKey , EmbeddingCollection ::from );
67122        cache .put (apiKey , result );
@@ -71,8 +126,8 @@ public void receiveInvalidateCache(final String apiKey) {
71126        cache .invalidate (apiKey );
72127    }
73128
74-     private  void  notifyCacheEvent (String  event , String  apiKey ) {
75-         CacheActionDto  cacheActionDto  = new  CacheActionDto (event , apiKey , SERVER_UUID );
129+     private  < T >  void  notifyCacheEvent (CacheAction  event , String  apiKey ,  T   action ) {
130+         CacheActionDto < T >  cacheActionDto  = new  CacheActionDto <> (event , apiKey , SERVER_UUID ,  action );
76131        notificationSenderService .notifyCacheChange (cacheActionDto );
77132    }
78133}
0 commit comments