1
1
package com .exadel .frs .core .trainservice .cache ;
2
2
3
+ import com .exadel .frs .commonservice .entity .Embedding ;
4
+ import com .exadel .frs .commonservice .projection .EmbeddingProjection ;
3
5
import 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 ;
4
11
import com .exadel .frs .core .trainservice .service .EmbeddingService ;
5
12
import com .exadel .frs .core .trainservice .service .NotificationSenderService ;
6
13
import com .google .common .cache .Cache ;
7
14
import 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 ;
12
17
import java .util .Optional ;
13
18
import java .util .concurrent .TimeUnit ;
14
19
import java .util .function .Consumer ;
20
+ import lombok .RequiredArgsConstructor ;
21
+ import lombok .extern .slf4j .Slf4j ;
22
+ import org .springframework .stereotype .Component ;
15
23
16
24
import static com .exadel .frs .core .trainservice .system .global .Constants .SERVER_UUID ;
17
25
@@ -34,34 +42,81 @@ public class EmbeddingCacheProvider {
34
42
.build ();
35
43
36
44
public EmbeddingCollection getOrLoad (final String apiKey ) {
37
-
38
45
var result = cache .getIfPresent (apiKey );
39
-
40
46
if (result == null ) {
41
47
result = embeddingService .doWithEnhancedEmbeddingProjectionStream (apiKey , EmbeddingCollection ::from );
42
-
43
48
cache .put (apiKey , result );
44
-
45
- notifyCacheEvent ("UPDATE" , apiKey );
46
49
}
47
-
48
50
return result ;
49
51
}
50
52
51
- public void ifPresent (String apiKey , Consumer < EmbeddingCollection > consumer ) {
53
+ public void removeEmbedding (String apiKey , EmbeddingProjection embedding ) {
52
54
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
+ }
54
66
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 );
57
108
}
58
109
59
110
public void invalidate (final String apiKey ) {
60
111
cache .invalidate (apiKey );
61
- notifyCacheEvent ("DELETE" , apiKey );
112
+ notifyCacheEvent (CacheAction . INVALIDATE , apiKey , null );
62
113
}
63
114
64
-
115
+ /**
116
+ * @deprecated
117
+ * See {@link com.exadel.frs.core.trainservice.service.NotificationHandler#handleUpdate(CacheActionDto)}
118
+ */
119
+ @ Deprecated (forRemoval = true )
65
120
public void receivePutOnCache (String apiKey ) {
66
121
var result = embeddingService .doWithEnhancedEmbeddingProjectionStream (apiKey , EmbeddingCollection ::from );
67
122
cache .put (apiKey , result );
@@ -71,8 +126,8 @@ public void receiveInvalidateCache(final String apiKey) {
71
126
cache .invalidate (apiKey );
72
127
}
73
128
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 );
76
131
notificationSenderService .notifyCacheChange (cacheActionDto );
77
132
}
78
133
}
0 commit comments