collections-cache 0.2.8.20250303__tar.gz → 0.3.0.20250303__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: collections-cache
3
- Version: 0.2.8.20250303
3
+ Version: 0.3.0.20250303
4
4
  Summary: collections-cache is a Python package for managing data collections across multiple SQLite databases. It allows efficient storage, retrieval, and updating of key-value pairs, supporting various data types serialized with pickle. The package uses parallel processing for fast access and manipulation of large collections.
5
5
  License: MIT
6
6
  Author: Luiz-Trindade
@@ -5,16 +5,17 @@ from itertools import chain
5
5
  from os import cpu_count, path, makedirs, scandir
6
6
  from concurrent.futures import ProcessPoolExecutor as Pool
7
7
  from concurrent.futures import ThreadPoolExecutor as Thread
8
- from threading import Thread as Thread_Exec
9
8
 
10
9
  class Collection_Cache:
11
- def __init__(self, collection_name):
10
+ def __init__(self, collection_name: str, size_limit:int = 800):
12
11
  # Variables
13
12
  self.collection_name = collection_name
14
13
  self.cpu_cores = cpu_count()
15
14
  self.collection_dir = path.join("./Collections", self.collection_name)
16
15
  self.databases_list = []
17
16
  self.keys_databases = {}
17
+ self.temp_keys_values = {}
18
+ self.size_limit = size_limit
18
19
 
19
20
  # Init methods
20
21
  self.create_collection()
@@ -66,13 +67,22 @@ class Collection_Cache:
66
67
  conn.close()
67
68
  return keys
68
69
 
69
- '''def set_key(self, key: str, value: any):
70
- """Used to store values and associate a value with a key."""
71
- t = Thread_Exec(target=self.set_key_exec, args=(key, value))
72
- t.start()
73
- t.join()'''
70
+ # Experimental
71
+ def verify_size_of_temp_queue(self, type_of_operation: str):
72
+ if type_of_operation == "set_key" and len(self.temp_keys_values) >= self.size_limit:
73
+ self.set_multi_keys(self.temp_keys_values)
74
+ self.temp_keys_values = {}
75
+ elif type_of_operation == "get_key":
76
+ self.set_multi_keys(self.temp_keys_values)
77
+ self.temp_keys_values = {}
74
78
 
79
+ # Experimental
75
80
  def set_key(self, key: str, value: any):
81
+ """Used to store values and associate a value with a key."""
82
+ self.temp_keys_values[key] = value
83
+ self.verify_size_of_temp_queue("set_key")
84
+
85
+ def set_key_exec(self, key: str, value: any):
76
86
  """Used to store values and associate a value with a key."""
77
87
  if key not in self.keys_databases:
78
88
  database_to_insert = choice(self.databases_list)
@@ -95,22 +105,20 @@ class Collection_Cache:
95
105
 
96
106
  def set_multi_keys(self, keys_and_values: dict[str, any]):
97
107
  """Experimental. Set multiple keys and values at the same time."""
98
-
99
108
  with Thread(self.cpu_cores) as thread:
100
- thread.map(lambda kv: self.set_key(kv[0], kv[1]), keys_and_values.items())
109
+ thread.map(lambda kv: self.set_key_exec(kv[0], kv[1]), keys_and_values.items())
101
110
 
102
111
  def add_to_keys_database(self, key, database):
103
112
  self.keys_databases[key] = database
104
113
 
105
114
  def delete_to_keys_database(self, key):
106
115
  """Removes the key from the dictionary of stored keys"""
107
-
108
116
  if key in self.keys_databases:
109
117
  del self.keys_databases[key]
110
118
 
111
119
  def get_key(self, key: str):
112
120
  """Used to obtain the value stored by the key"""
113
-
121
+ self.verify_size_of_temp_queue("get_key")
114
122
  try:
115
123
  database_to_search = self.keys_databases[key]
116
124
  conn = sqlite3.connect(database_to_search)
@@ -122,11 +130,10 @@ class Collection_Cache:
122
130
  return pickle.loads(result[0][0])
123
131
 
124
132
  except Exception as error:
125
- return error
133
+ return None
126
134
 
127
135
  def delete_key(self, key: str):
128
136
  """Used to delete the value stored by the key"""
129
-
130
137
  try:
131
138
  database_to_delete = self.keys_databases[key]
132
139
  conn = sqlite3.connect(database_to_delete)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "collections-cache"
3
- version = "0.2.8.20250303"
3
+ version = "0.3.0.20250303"
4
4
  description = "collections-cache is a Python package for managing data collections across multiple SQLite databases. It allows efficient storage, retrieval, and updating of key-value pairs, supporting various data types serialized with pickle. The package uses parallel processing for fast access and manipulation of large collections."
5
5
  authors = ["Luiz-Trindade <luiz.gabriel.m.trindade@gmail.com>"]
6
6
  license = "MIT"