77import collections .abc
88import datetime
99import errno
10- import json
1110import logging
1211import os
1312import sqlite3
1817
1918import platformdirs
2019
20+ from qlient .settings import Settings
2121from qlient .schema .types import RawSchema
2222
2323logger = logging .getLogger ("qlient" )
@@ -120,30 +120,11 @@ class SqliteCache(Cache):
120120 LENGTH_STMT = f"SELECT COUNT(URL) AS CACHE_SIZE FROM { TABLE_NAME } " # skipcq: BAN-B608
121121 ITER_STMT = f"SELECT URL, SCHEMA FROM { TABLE_NAME } " # skipcq: BAN-B608
122122
123- @staticmethod
124- def _encode_schema (schema : RawSchema ) -> str :
125- """Static method to encode the raw schema before inserting it into the database
126-
127- :param schema: holds the raw schema
128- :return: a base64 encoded representation of the schema
129- """
130- schema_string = json .dumps (schema , ensure_ascii = False )
131- return base64 .b64encode (schema_string .encode ()).decode ()
132-
133- @staticmethod
134- def _decode_schema (encoded_schema : str ) -> RawSchema :
135- """Static method to decode the base64 encoded schema back to the dictionary
136-
137- :param encoded_schema: holds a base64 representation of the schema
138- :return: a raw schema instance
139- """
140- decoded_raw_schema = base64 .b64decode (encoded_schema .encode ()).decode ()
141- return json .loads (decoded_raw_schema )
142-
143123 def __init__ (
144124 self ,
145125 path : Optional [str ] = None ,
146- expires_in : Union [int , datetime .timedelta ] = ONE_HOUR
126+ expires_in : Union [int , datetime .timedelta ] = ONE_HOUR ,
127+ settings : Optional [Settings ] = None ,
147128 ):
148129 """Initialize a new sqlite cache
149130
@@ -156,6 +137,7 @@ def __init__(
156137 + "Please use qlient.cache.InMemoryCache()."
157138 )
158139
140+ self .settings : Settings = settings if settings is not None else Settings ()
159141 self .path : str = path or _get_default_sqlite_cache_file ()
160142
161143 if isinstance (expires_in , int ):
@@ -186,6 +168,24 @@ def create_cache_table_if_not_exists(self):
186168 cursor .execute (self .TABLE_STMT )
187169 connection .commit ()
188170
171+ def _encode_schema (self , schema : RawSchema ) -> str :
172+ """Static method to encode the raw schema before inserting it into the database
173+
174+ :param schema: holds the raw schema
175+ :return: a base64 encoded representation of the schema
176+ """
177+ schema_string = self .settings .json_dumps (schema , ensure_ascii = False )
178+ return base64 .b64encode (schema_string .encode ()).decode ()
179+
180+ def _decode_schema (self , encoded_schema : str ) -> RawSchema :
181+ """Static method to decode the base64 encoded schema back to the dictionary
182+
183+ :param encoded_schema: holds a base64 representation of the schema
184+ :return: a raw schema instance
185+ """
186+ decoded_raw_schema = base64 .b64decode (encoded_schema .encode ()).decode ()
187+ return self .settings .json_loads (decoded_raw_schema )
188+
189189 def __setitem__ (self , url : str , schema : RawSchema ):
190190 logger .debug (f"Sqlite caching schema for url `{ url } `" )
191191 encoded_schema = self ._encode_schema (schema )
0 commit comments