diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 47e313a..872e181 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -36,7 +36,7 @@ jobs: - name: Install dependencies run: | - pip install pytest pytest-cov mysql-connector-python Pympler + pip install pytest pytest-cov mysql-connector-python Pympler orjson - name: Set up MySQL client run: sudo apt-get install mysql-client diff --git a/mybatis/cache.py b/mybatis/cache.py index 7962b4c..5f140ec 100644 --- a/mybatis/cache.py +++ b/mybatis/cache.py @@ -1,5 +1,4 @@ -import json -import pickle +import orjson as json import time from typing import Dict, Any, Optional diff --git a/setup.py b/setup.py index 7bc8187..e242437 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,8 @@ packages=find_packages(), install_requires=[ 'mysql-connector-python>=9.0.0', - 'Pympler>=1.1' + 'Pympler>=1.1', + 'orjson>=3.10.12' ], classifiers=[ 'Programming Language :: Python :: 3', diff --git a/test/test_cache.py b/test/test_cache.py index c9c5986..8e4df2d 100644 --- a/test/test_cache.py +++ b/test/test_cache.py @@ -4,12 +4,14 @@ from mybatis import Cache, CacheKey def test_basic(): - cache = Cache(memory_limit=555, max_live_ms=10*1000) # 50MB, 10sec + cache = Cache(memory_limit=400, max_live_ms=10*1000) # 50MB, 10sec cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}]) cache.put(CacheKey("b", [2, 'b', None]), "2") cache.put(CacheKey("c", [3, 'c', None]), "3") cache.put(CacheKey("d", [4, 'd', None]), None) + print ("============> memory_used:", cache.memory_used) + assert cache.get(CacheKey('a', [1, 'a', None])) == None assert cache.get(CacheKey('b', [2, 'b', None])) == '2' @@ -18,13 +20,13 @@ def test_basic(): l.append((key, value, memory_usage, type(value))) assert len(l) == 3 - assert l[0][0] == '{"sql": "b", "param_list": [2, "b", null]}' + assert l[0][0] == b'{"sql":"b","param_list":[2,"b",null]}' assert l[0][1] == '2' - assert l[1][0] == '{"sql": "d", "param_list": [4, "d", null]}' + assert l[1][0] == b'{"sql":"d","param_list":[4,"d",null]}' assert l[1][1] == None - assert l[2][0] == '{"sql": "c", "param_list": [3, "c", null]}' + assert l[2][0] == b'{"sql":"c","param_list":[3,"c",null]}' assert l[2][1] == '3' cache.put(CacheKey("e", [5, 'e', None]), "5") @@ -36,17 +38,17 @@ def test_basic(): assert len(l) == 3 - assert l[0][0] == '{"sql": "e", "param_list": [5, "e", null]}' + assert l[0][0] == b'{"sql":"e","param_list":[5,"e",null]}' assert l[0][1] == '5' - assert l[1][0] == '{"sql": "b", "param_list": [2, "b", null]}' + assert l[1][0] == b'{"sql":"b","param_list":[2,"b",null]}' assert l[1][1] == '2' - assert l[2][0] == '{"sql": "d", "param_list": [4, "d", null]}' + assert l[2][0] == b'{"sql":"d","param_list":[4,"d",null]}' assert l[2][1] == None def test_timeout(): - cache = Cache(memory_limit=555, max_live_ms=1 * 1000) # 50MB, 10sec + cache = Cache(memory_limit=400, max_live_ms=1 * 1000) # 50MB, 10sec cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}]) cache.put(CacheKey("b", [2, 'b', None]), "2") cache.put(CacheKey("c", [3, 'c', None]), "3") @@ -61,7 +63,7 @@ def test_timeout(): assert cache.memory_used == 0 def test_overwrite(): - cache = Cache(memory_limit=180, max_live_ms=10 * 1000) # 50MB, 10sec + cache = Cache(memory_limit=120, max_live_ms=10 * 1000) # 50MB, 10sec cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}]) #print ("++++>cache.memory_used:", cache.memory_used) cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}, {"a3":3}])