@@ -41,15 +41,15 @@ def _insert(self, data: List):
41
41
model = data [3 ]
42
42
answer_type = 0
43
43
embedding_data = embedding_data .tobytes ()
44
+ is_deleted = 0
44
45
45
- table_name = "cache_codegpt_answer"
46
- insert_sql = "INSERT INTO {} (question, answer, answer_type, model, embedding_data) VALUES (%s, %s, %s, %s, _binary%s)" .format (table_name )
47
-
46
+ table_name = "modelcache_llm_answer"
47
+ insert_sql = "INSERT INTO {} (question, answer, answer_type, model, embedding_data, is_deleted) VALUES (%s, %s, %s, %s, _binary%s, %s)" .format (table_name )
48
48
conn = self .pool .connection ()
49
49
try :
50
50
with conn .cursor () as cursor :
51
51
# 执行插入数据操作
52
- values = (question , answer , answer_type , model , embedding_data )
52
+ values = (question , answer , answer_type , model , embedding_data , is_deleted )
53
53
cursor .execute (insert_sql , values )
54
54
conn .commit ()
55
55
id = cursor .lastrowid
@@ -91,7 +91,7 @@ def insert_query_resp(self, query_resp, **kwargs):
91
91
conn .close ()
92
92
93
93
def get_data_by_id (self , key : int ):
94
- table_name = "cache_codegpt_answer "
94
+ table_name = "modelcache_llm_answer "
95
95
query_sql = "select question, answer, embedding_data, model from {} where id={}" .format (table_name , key )
96
96
conn_start = time .time ()
97
97
conn = self .pool .connection ()
@@ -112,7 +112,7 @@ def get_data_by_id(self, key: int):
112
112
return None
113
113
114
114
def update_hit_count_by_id (self , primary_id : int ):
115
- table_name = "cache_codegpt_answer "
115
+ table_name = "modelcache_llm_answer "
116
116
update_sql = "UPDATE {} SET hit_count = hit_count+1 WHERE id={}" .format (table_name , primary_id )
117
117
conn = self .pool .connection ()
118
118
@@ -127,18 +127,30 @@ def update_hit_count_by_id(self, primary_id: int):
127
127
conn .close ()
128
128
129
129
def get_ids (self , deleted = True ):
130
- pass
130
+ table_name = "modelcache_llm_answer"
131
+ state = 1 if deleted else 0
132
+ query_sql = "Select id FROM {} WHERE is_deleted = {}" .format (table_name , state )
133
+
134
+ conn = self .pool .connection ()
135
+ try :
136
+ with conn .cursor () as cursor :
137
+ cursor .execute (query_sql )
138
+ ids = [row [0 ] for row in cursor .fetchall ()]
139
+ finally :
140
+ conn .close ()
141
+
142
+ return ids
131
143
132
144
def mark_deleted (self , keys ):
133
- table_name = "cache_codegpt_answer "
134
- delete_sql = "Delete from {} WHERE id in ({})" .format (table_name , "," .join ([str (i ) for i in keys ]))
145
+ table_name = "modelcache_llm_answer "
146
+ mark_sql = " update {} set is_deleted=1 WHERE id in ({})" .format (table_name , "," .join ([str (i ) for i in keys ]))
135
147
136
148
# 从连接池中获取连接
137
149
conn = self .pool .connection ()
138
150
try :
139
151
with conn .cursor () as cursor :
140
152
# 执行删除数据操作
141
- cursor .execute (delete_sql )
153
+ cursor .execute (mark_sql )
142
154
delete_count = cursor .rowcount
143
155
conn .commit ()
144
156
finally :
@@ -147,7 +159,7 @@ def mark_deleted(self, keys):
147
159
return delete_count
148
160
149
161
def model_deleted (self , model_name ):
150
- table_name = "cache_codegpt_answer "
162
+ table_name = "modelcache_llm_answer "
151
163
delete_sql = "Delete from {} WHERE model='{}'" .format (table_name , model_name )
152
164
153
165
table_log_name = "modelcache_query_log"
@@ -169,10 +181,36 @@ def model_deleted(self, model_name):
169
181
return resp
170
182
171
183
def clear_deleted_data (self ):
172
- pass
184
+ table_name = "modelcache_llm_answer"
185
+ delete_sql = "DELETE FROM {} WHERE is_deleted = 1" .format (table_name )
186
+
187
+ conn = self .pool .connection ()
188
+ try :
189
+ with conn .cursor () as cursor :
190
+ cursor .execute (delete_sql )
191
+ delete_count = cursor .rowcount
192
+ conn .commit ()
193
+ finally :
194
+ conn .close ()
195
+
196
+ return delete_count
173
197
174
198
def count (self , state : int = 0 , is_all : bool = False ):
175
- pass
199
+ table_name = "modelcache_llm_answer"
200
+ if is_all :
201
+ count_sql = "SELECT COUNT(*) FROM {}" .format (table_name )
202
+ else :
203
+ count_sql = "SELECT COUNT(*) FROM {} WHERE is_deleted = {}" .format (table_name ,state )
204
+
205
+ conn = self .pool .connection ()
206
+ try :
207
+ with conn .cursor () as cursor :
208
+ cursor .execute (count_sql )
209
+ num = cursor .fetchone ()[0 ]
210
+ finally :
211
+ conn .close ()
212
+
213
+ return num
176
214
177
215
def close (self ):
178
216
pass
0 commit comments