collections-cache 0.2.7.20250303__tar.gz → 0.2.9.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.
@@ -0,0 +1,155 @@
1
+ Metadata-Version: 2.1
2
+ Name: collections-cache
3
+ Version: 0.2.9.20250303
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
+ License: MIT
6
+ Author: Luiz-Trindade
7
+ Author-email: luiz.gabriel.m.trindade@gmail.com
8
+ Requires-Python: >=3.12,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Description-Content-Type: text/markdown
13
+
14
+ # collections-cache 🚀
15
+
16
+ `collections-cache` is a fast and scalable key–value caching solution built on top of SQLite. It allows you to store, update, and retrieve data using unique keys, and it supports complex Python data types (thanks to `pickle`). Designed to harness the power of multiple CPU cores, the library shards data across multiple SQLite databases, enabling impressive performance scaling.
17
+
18
+ ---
19
+
20
+ ## Features ✨
21
+
22
+ - **Multiple SQLite Databases**: Distributes your data across several databases to optimize I/O and take advantage of multi-core systems.
23
+ - **Key–Value Store**: Simple and intuitive interface for storing and retrieving data.
24
+ - **Supports Complex Data Types**: Serialize and store lists, dictionaries, objects, and more using `pickle`.
25
+ - **Parallel Processing**: Uses Python’s `multiprocessing` and `concurrent.futures` modules to perform operations in parallel.
26
+ - **Efficient Data Retrieval**: Caches all keys in memory for super-fast lookups.
27
+ - **Cross-Platform**: Runs on Linux, macOS, and Windows.
28
+ - **Performance Scaling**: Benchmarks show near-linear scaling with the number of real CPU cores.
29
+
30
+ ---
31
+
32
+ ## Installation 📦
33
+
34
+ Use [Poetry](https://python-poetry.org/) to install and manage dependencies:
35
+
36
+ 1. Clone the repository:
37
+
38
+ ```bash
39
+ git clone https://github.com/Luiz-Trindade/collections_cache.git
40
+ cd collections-cache
41
+ ```
42
+
43
+ 2. Install the package with Poetry:
44
+
45
+ ```bash
46
+ poetry install
47
+ ```
48
+
49
+ ---
50
+
51
+ ## Usage ⚙️
52
+
53
+ Simply import and start using the main class, `Collection_Cache`, to interact with your collection:
54
+
55
+ ### Basic Example
56
+
57
+ ```python
58
+ from collections_cache import Collection_Cache
59
+
60
+ # Create a new collection named "STORE"
61
+ cache = Collection_Cache("STORE")
62
+
63
+ # Set a key-value pair
64
+ cache.set_key("products", ["apple", "orange", "onion"])
65
+
66
+ # Retrieve the value by key
67
+ products = cache.get_key("products")
68
+ print(products) # Output: ['apple', 'orange', 'onion']
69
+ ```
70
+
71
+ ### Bulk Insertion Example
72
+
73
+ For faster insertions, accumulate your data and use `set_multi_keys`:
74
+
75
+ ```python
76
+ from collections_cache import Collection_Cache
77
+ from random import uniform, randint
78
+ from time import time
79
+
80
+ cache = Collection_Cache("web_cache")
81
+ insertions = 100_000
82
+ data = {}
83
+
84
+ # Generate data
85
+ for i in range(insertions):
86
+ key = str(uniform(0.0, 100.0))
87
+ value = "some text :)" * randint(1, 100)
88
+ data[key] = value
89
+
90
+ # Bulk insert keys using multi-threaded execution
91
+ cache.set_multi_keys(data)
92
+
93
+ print(f"Inserted {len(cache.keys())} keys successfully!")
94
+ ```
95
+
96
+ ---
97
+
98
+ ## API Overview 📚
99
+
100
+ - **`set_key(key, value)`**: Stores a key–value pair. Updates the value if the key already exists.
101
+ - **`set_multi_keys(key_and_value)`**: (Experimental) Inserts multiple key–value pairs in parallel.
102
+ - **`get_key(key)`**: Retrieves the value associated with a given key.
103
+ - **`delete_key(key)`**: Removes a key and its corresponding value.
104
+ - **`keys()`**: Returns a list of all stored keys.
105
+ - **`export_to_json()`**: (Future feature) Exports your collection to a JSON file.
106
+
107
+ ---
108
+
109
+ ## Performance Benchmark 📊
110
+
111
+ On a machine with 4 real CPU cores, benchmarks indicate around **781 insertions per second**. The library is designed to scale nearly linearly with the number of real cores. For example:
112
+ - **6 cores**: ~1,171 insertions per second.
113
+ - **16 cores**: ~3,125 insertions per second.
114
+ - **128 cores**: ~25,000 insertions per second (theoretically).
115
+
116
+ *Note: Actual performance will depend on disk I/O, SQLite contention, and system architecture.*
117
+
118
+ ---
119
+
120
+ ## Development & Contributing 👩‍💻👨‍💻
121
+
122
+ To contribute or run tests:
123
+
124
+ 1. Install development dependencies:
125
+
126
+ ```bash
127
+ poetry install --dev
128
+ ```
129
+
130
+ 2. Run tests using:
131
+
132
+ ```bash
133
+ poetry run pytest
134
+ ```
135
+
136
+ Feel free to submit issues, pull requests, or feature suggestions. Your contributions help make `collections-cache` even better!
137
+
138
+ ---
139
+
140
+ ## License 📄
141
+
142
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
143
+
144
+ ---
145
+
146
+ ## Acknowledgements 🙌
147
+
148
+ - Inspired by the need for efficient, multi-core caching with SQLite.
149
+ - Created by Luiz Trindade.
150
+ - Thanks to all contributors and users who provide feedback to keep improving the library!
151
+
152
+ ---
153
+
154
+ Give `collections-cache` a try and let it power your high-performance caching needs! 🚀
155
+
@@ -0,0 +1,141 @@
1
+ # collections-cache 🚀
2
+
3
+ `collections-cache` is a fast and scalable key–value caching solution built on top of SQLite. It allows you to store, update, and retrieve data using unique keys, and it supports complex Python data types (thanks to `pickle`). Designed to harness the power of multiple CPU cores, the library shards data across multiple SQLite databases, enabling impressive performance scaling.
4
+
5
+ ---
6
+
7
+ ## Features ✨
8
+
9
+ - **Multiple SQLite Databases**: Distributes your data across several databases to optimize I/O and take advantage of multi-core systems.
10
+ - **Key–Value Store**: Simple and intuitive interface for storing and retrieving data.
11
+ - **Supports Complex Data Types**: Serialize and store lists, dictionaries, objects, and more using `pickle`.
12
+ - **Parallel Processing**: Uses Python’s `multiprocessing` and `concurrent.futures` modules to perform operations in parallel.
13
+ - **Efficient Data Retrieval**: Caches all keys in memory for super-fast lookups.
14
+ - **Cross-Platform**: Runs on Linux, macOS, and Windows.
15
+ - **Performance Scaling**: Benchmarks show near-linear scaling with the number of real CPU cores.
16
+
17
+ ---
18
+
19
+ ## Installation 📦
20
+
21
+ Use [Poetry](https://python-poetry.org/) to install and manage dependencies:
22
+
23
+ 1. Clone the repository:
24
+
25
+ ```bash
26
+ git clone https://github.com/Luiz-Trindade/collections_cache.git
27
+ cd collections-cache
28
+ ```
29
+
30
+ 2. Install the package with Poetry:
31
+
32
+ ```bash
33
+ poetry install
34
+ ```
35
+
36
+ ---
37
+
38
+ ## Usage ⚙️
39
+
40
+ Simply import and start using the main class, `Collection_Cache`, to interact with your collection:
41
+
42
+ ### Basic Example
43
+
44
+ ```python
45
+ from collections_cache import Collection_Cache
46
+
47
+ # Create a new collection named "STORE"
48
+ cache = Collection_Cache("STORE")
49
+
50
+ # Set a key-value pair
51
+ cache.set_key("products", ["apple", "orange", "onion"])
52
+
53
+ # Retrieve the value by key
54
+ products = cache.get_key("products")
55
+ print(products) # Output: ['apple', 'orange', 'onion']
56
+ ```
57
+
58
+ ### Bulk Insertion Example
59
+
60
+ For faster insertions, accumulate your data and use `set_multi_keys`:
61
+
62
+ ```python
63
+ from collections_cache import Collection_Cache
64
+ from random import uniform, randint
65
+ from time import time
66
+
67
+ cache = Collection_Cache("web_cache")
68
+ insertions = 100_000
69
+ data = {}
70
+
71
+ # Generate data
72
+ for i in range(insertions):
73
+ key = str(uniform(0.0, 100.0))
74
+ value = "some text :)" * randint(1, 100)
75
+ data[key] = value
76
+
77
+ # Bulk insert keys using multi-threaded execution
78
+ cache.set_multi_keys(data)
79
+
80
+ print(f"Inserted {len(cache.keys())} keys successfully!")
81
+ ```
82
+
83
+ ---
84
+
85
+ ## API Overview 📚
86
+
87
+ - **`set_key(key, value)`**: Stores a key–value pair. Updates the value if the key already exists.
88
+ - **`set_multi_keys(key_and_value)`**: (Experimental) Inserts multiple key–value pairs in parallel.
89
+ - **`get_key(key)`**: Retrieves the value associated with a given key.
90
+ - **`delete_key(key)`**: Removes a key and its corresponding value.
91
+ - **`keys()`**: Returns a list of all stored keys.
92
+ - **`export_to_json()`**: (Future feature) Exports your collection to a JSON file.
93
+
94
+ ---
95
+
96
+ ## Performance Benchmark 📊
97
+
98
+ On a machine with 4 real CPU cores, benchmarks indicate around **781 insertions per second**. The library is designed to scale nearly linearly with the number of real cores. For example:
99
+ - **6 cores**: ~1,171 insertions per second.
100
+ - **16 cores**: ~3,125 insertions per second.
101
+ - **128 cores**: ~25,000 insertions per second (theoretically).
102
+
103
+ *Note: Actual performance will depend on disk I/O, SQLite contention, and system architecture.*
104
+
105
+ ---
106
+
107
+ ## Development & Contributing 👩‍💻👨‍💻
108
+
109
+ To contribute or run tests:
110
+
111
+ 1. Install development dependencies:
112
+
113
+ ```bash
114
+ poetry install --dev
115
+ ```
116
+
117
+ 2. Run tests using:
118
+
119
+ ```bash
120
+ poetry run pytest
121
+ ```
122
+
123
+ Feel free to submit issues, pull requests, or feature suggestions. Your contributions help make `collections-cache` even better!
124
+
125
+ ---
126
+
127
+ ## License 📄
128
+
129
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
130
+
131
+ ---
132
+
133
+ ## Acknowledgements 🙌
134
+
135
+ - Inspired by the need for efficient, multi-core caching with SQLite.
136
+ - Created by Luiz Trindade.
137
+ - Thanks to all contributors and users who provide feedback to keep improving the library!
138
+
139
+ ---
140
+
141
+ Give `collections-cache` a try and let it power your high-performance caching needs! 🚀
@@ -15,6 +15,8 @@ class Collection_Cache:
15
15
  self.collection_dir = path.join("./Collections", self.collection_name)
16
16
  self.databases_list = []
17
17
  self.keys_databases = {}
18
+ self.temp_keys_values = {}
19
+ self.size_limit = 200
18
20
 
19
21
  # Init methods
20
22
  self.create_collection()
@@ -66,13 +68,22 @@ class Collection_Cache:
66
68
  conn.close()
67
69
  return keys
68
70
 
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()'''
71
+ # Experimental
72
+ def verify_size_of_temp_queue(self, type_of_operation: str):
73
+ if type_of_operation == "set_key" and len(self.temp_keys_values) >= self.size_limit:
74
+ self.set_multi_keys(self.temp_keys_values)
75
+ self.temp_keys_values = {}
76
+ else:
77
+ self.set_multi_keys(self.temp_keys_values)
78
+ self.temp_keys_values = {}
74
79
 
80
+ # Experimental
75
81
  def set_key(self, key: str, value: any):
82
+ """Used to store values and associate a value with a key."""
83
+ self.temp_keys_values[key] = value
84
+ self.verify_size_of_temp_queue("set_key")
85
+
86
+ def set_key_exec(self, key: str, value: any):
76
87
  """Used to store values and associate a value with a key."""
77
88
  if key not in self.keys_databases:
78
89
  database_to_insert = choice(self.databases_list)
@@ -95,22 +106,20 @@ class Collection_Cache:
95
106
 
96
107
  def set_multi_keys(self, keys_and_values: dict[str, any]):
97
108
  """Experimental. Set multiple keys and values at the same time."""
98
-
99
109
  with Thread(self.cpu_cores) as thread:
100
- thread.map(lambda kv: self.set_key(kv[0], kv[1]), keys_and_values.items())
110
+ thread.map(lambda kv: self.set_key_exec(kv[0], kv[1]), keys_and_values.items())
101
111
 
102
112
  def add_to_keys_database(self, key, database):
103
113
  self.keys_databases[key] = database
104
114
 
105
115
  def delete_to_keys_database(self, key):
106
116
  """Removes the key from the dictionary of stored keys"""
107
-
108
117
  if key in self.keys_databases:
109
118
  del self.keys_databases[key]
110
119
 
111
120
  def get_key(self, key: str):
112
121
  """Used to obtain the value stored by the key"""
113
-
122
+ self.verify_size_of_temp_queue("get_key")
114
123
  try:
115
124
  database_to_search = self.keys_databases[key]
116
125
  conn = sqlite3.connect(database_to_search)
@@ -122,11 +131,10 @@ class Collection_Cache:
122
131
  return pickle.loads(result[0][0])
123
132
 
124
133
  except Exception as error:
125
- return error
134
+ return None
126
135
 
127
136
  def delete_key(self, key: str):
128
137
  """Used to delete the value stored by the key"""
129
-
130
138
  try:
131
139
  database_to_delete = self.keys_databases[key]
132
140
  conn = sqlite3.connect(database_to_delete)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "collections-cache"
3
- version = "0.2.7.20250303"
3
+ version = "0.2.9.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"
@@ -1,96 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: collections-cache
3
- Version: 0.2.7.20250303
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
- License: MIT
6
- Author: Luiz-Trindade
7
- Author-email: luiz.gabriel.m.trindade@gmail.com
8
- Requires-Python: >=3.12,<4.0
9
- Classifier: License :: OSI Approved :: MIT License
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.12
12
- Description-Content-Type: text/markdown
13
-
14
- # collections-cache
15
-
16
- `collections-cache` is a simple and efficient caching solution built with SQLite databases. It allows storing, updating, and retrieving data using unique keys while supporting complex data types through `pickle`. Designed to scale across multiple CPU cores, it distributes data across multiple SQLite databases for improved performance.
17
-
18
- ## Features
19
-
20
- - **Multiple SQLite databases**: Distributes data across multiple databases for better scalability.
21
- - **Key-value store**: Stores data as key-value pairs.
22
- - **Supports complex data types**: Data is serialized using `pickle`, allowing you to store lists, dictionaries, and other Python objects.
23
- - **Parallel processing**: Utilizes Python’s `multiprocessing` module to handle large collections in parallel across multiple CPU cores.
24
- - **Efficient data retrieval**: Retrieves stored data efficiently based on the key.
25
- - **Cross-platform**: Works on Linux, macOS, and Windows.
26
-
27
- ## Installation
28
-
29
- To install the `collections-cache` package, use [Poetry](https://python-poetry.org/) to manage dependencies.
30
-
31
- 1. Clone the repository:
32
-
33
- ```bash
34
- git clone https://github.com/Luiz-Trindade/collections_cache.git
35
- cd collections-cache
36
- ```
37
-
38
- 2. Install the package with Poetry:
39
-
40
- ```bash
41
- poetry install
42
- ```
43
-
44
- ## Usage
45
-
46
- To use the `collections-cache` package, import the main class `Collection_Cache` and interact with your collection.
47
-
48
- ### Example
49
-
50
- ```python
51
- from collections_cache import Collection_Cache
52
-
53
- # Create a new collection
54
- cache = Collection_Cache("STORE")
55
-
56
- # Set a key-value pair
57
- cache.set_key("products", ["apple", "orange", "onion"])
58
-
59
- # Get the value by key
60
- products = cache.get_key("products")
61
- print(products) # Output: ['apple', 'orange', 'onion']
62
- ```
63
-
64
- ### Methods
65
-
66
- - **`set_key(key, value)`**: Stores a key-value pair in the cache. If the key already exists, its value is updated.
67
- - **`set_multi_keys(key_and_value)`**: Stores a multi key-value pair in the cache. If the key already exists, its value is updated.
68
- - **`get_key(key)`**: Retrieves the value associated with a key.
69
- - **`delete_key(key)`**: Removes an existing key from the cache.
70
- - **`keys`***: Returns all stored keys.
71
-
72
- ## Development
73
-
74
- To contribute or run tests:
75
-
76
- 1. Install development dependencies:
77
-
78
- ```bash
79
- poetry install --dev
80
- ```
81
-
82
- 2. Run tests:
83
-
84
- ```bash
85
- poetry run pytest
86
- ```
87
-
88
- ## License
89
-
90
- This project is licensed under the MIT License – see the [LICENSE](LICENSE) file for details.
91
-
92
- ## Acknowledgements
93
-
94
- - This package was created to demonstrate how to work with SQLite, `pickle`, and Python's `multiprocessing` module.
95
- - Created by: Luiz Trindade.
96
-
@@ -1,82 +0,0 @@
1
- # collections-cache
2
-
3
- `collections-cache` is a simple and efficient caching solution built with SQLite databases. It allows storing, updating, and retrieving data using unique keys while supporting complex data types through `pickle`. Designed to scale across multiple CPU cores, it distributes data across multiple SQLite databases for improved performance.
4
-
5
- ## Features
6
-
7
- - **Multiple SQLite databases**: Distributes data across multiple databases for better scalability.
8
- - **Key-value store**: Stores data as key-value pairs.
9
- - **Supports complex data types**: Data is serialized using `pickle`, allowing you to store lists, dictionaries, and other Python objects.
10
- - **Parallel processing**: Utilizes Python’s `multiprocessing` module to handle large collections in parallel across multiple CPU cores.
11
- - **Efficient data retrieval**: Retrieves stored data efficiently based on the key.
12
- - **Cross-platform**: Works on Linux, macOS, and Windows.
13
-
14
- ## Installation
15
-
16
- To install the `collections-cache` package, use [Poetry](https://python-poetry.org/) to manage dependencies.
17
-
18
- 1. Clone the repository:
19
-
20
- ```bash
21
- git clone https://github.com/Luiz-Trindade/collections_cache.git
22
- cd collections-cache
23
- ```
24
-
25
- 2. Install the package with Poetry:
26
-
27
- ```bash
28
- poetry install
29
- ```
30
-
31
- ## Usage
32
-
33
- To use the `collections-cache` package, import the main class `Collection_Cache` and interact with your collection.
34
-
35
- ### Example
36
-
37
- ```python
38
- from collections_cache import Collection_Cache
39
-
40
- # Create a new collection
41
- cache = Collection_Cache("STORE")
42
-
43
- # Set a key-value pair
44
- cache.set_key("products", ["apple", "orange", "onion"])
45
-
46
- # Get the value by key
47
- products = cache.get_key("products")
48
- print(products) # Output: ['apple', 'orange', 'onion']
49
- ```
50
-
51
- ### Methods
52
-
53
- - **`set_key(key, value)`**: Stores a key-value pair in the cache. If the key already exists, its value is updated.
54
- - **`set_multi_keys(key_and_value)`**: Stores a multi key-value pair in the cache. If the key already exists, its value is updated.
55
- - **`get_key(key)`**: Retrieves the value associated with a key.
56
- - **`delete_key(key)`**: Removes an existing key from the cache.
57
- - **`keys`***: Returns all stored keys.
58
-
59
- ## Development
60
-
61
- To contribute or run tests:
62
-
63
- 1. Install development dependencies:
64
-
65
- ```bash
66
- poetry install --dev
67
- ```
68
-
69
- 2. Run tests:
70
-
71
- ```bash
72
- poetry run pytest
73
- ```
74
-
75
- ## License
76
-
77
- This project is licensed under the MIT License – see the [LICENSE](LICENSE) file for details.
78
-
79
- ## Acknowledgements
80
-
81
- - This package was created to demonstrate how to work with SQLite, `pickle`, and Python's `multiprocessing` module.
82
- - Created by: Luiz Trindade.