Skip to content

Commit b426e82

Browse files
committed
Replace json to orjson for performance.
1 parent 05471d3 commit b426e82

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636

3737
- name: Install dependencies
3838
run: |
39-
pip install pytest pytest-cov mysql-connector-python Pympler
39+
pip install pytest pytest-cov mysql-connector-python Pympler orjson
4040
4141
- name: Set up MySQL client
4242
run: sudo apt-get install mysql-client

mybatis/cache.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import json
2-
import pickle
1+
import orjson as json
32
import time
43
from typing import Dict, Any, Optional
54

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
packages=find_packages(),
1313
install_requires=[
1414
'mysql-connector-python>=9.0.0',
15-
'Pympler>=1.1'
15+
'Pympler>=1.1',
16+
'orjson>=3.10.12'
1617
],
1718
classifiers=[
1819
'Programming Language :: Python :: 3',

test/test_cache.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from mybatis import Cache, CacheKey
55

66
def test_basic():
7-
cache = Cache(memory_limit=555, max_live_ms=10*1000) # 50MB, 10sec
7+
cache = Cache(memory_limit=400, max_live_ms=10*1000) # 50MB, 10sec
88
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}])
99
cache.put(CacheKey("b", [2, 'b', None]), "2")
1010
cache.put(CacheKey("c", [3, 'c', None]), "3")
1111
cache.put(CacheKey("d", [4, 'd', None]), None)
1212

13+
print ("============> memory_used:", cache.memory_used)
14+
1315
assert cache.get(CacheKey('a', [1, 'a', None])) == None
1416
assert cache.get(CacheKey('b', [2, 'b', None])) == '2'
1517

@@ -18,13 +20,13 @@ def test_basic():
1820
l.append((key, value, memory_usage, type(value)))
1921

2022
assert len(l) == 3
21-
assert l[0][0] == '{"sql": "b", "param_list": [2, "b", null]}'
23+
assert l[0][0] == b'{"sql":"b","param_list":[2,"b",null]}'
2224
assert l[0][1] == '2'
2325

24-
assert l[1][0] == '{"sql": "d", "param_list": [4, "d", null]}'
26+
assert l[1][0] == b'{"sql":"d","param_list":[4,"d",null]}'
2527
assert l[1][1] == None
2628

27-
assert l[2][0] == '{"sql": "c", "param_list": [3, "c", null]}'
29+
assert l[2][0] == b'{"sql":"c","param_list":[3,"c",null]}'
2830
assert l[2][1] == '3'
2931

3032
cache.put(CacheKey("e", [5, 'e', None]), "5")
@@ -36,17 +38,17 @@ def test_basic():
3638

3739
assert len(l) == 3
3840

39-
assert l[0][0] == '{"sql": "e", "param_list": [5, "e", null]}'
41+
assert l[0][0] == b'{"sql":"e","param_list":[5,"e",null]}'
4042
assert l[0][1] == '5'
4143

42-
assert l[1][0] == '{"sql": "b", "param_list": [2, "b", null]}'
44+
assert l[1][0] == b'{"sql":"b","param_list":[2,"b",null]}'
4345
assert l[1][1] == '2'
4446

45-
assert l[2][0] == '{"sql": "d", "param_list": [4, "d", null]}'
47+
assert l[2][0] == b'{"sql":"d","param_list":[4,"d",null]}'
4648
assert l[2][1] == None
4749

4850
def test_timeout():
49-
cache = Cache(memory_limit=555, max_live_ms=1 * 1000) # 50MB, 10sec
51+
cache = Cache(memory_limit=400, max_live_ms=1 * 1000) # 50MB, 10sec
5052
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}])
5153
cache.put(CacheKey("b", [2, 'b', None]), "2")
5254
cache.put(CacheKey("c", [3, 'c', None]), "3")
@@ -61,7 +63,7 @@ def test_timeout():
6163
assert cache.memory_used == 0
6264

6365
def test_overwrite():
64-
cache = Cache(memory_limit=180, max_live_ms=10 * 1000) # 50MB, 10sec
66+
cache = Cache(memory_limit=120, max_live_ms=10 * 1000) # 50MB, 10sec
6567
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}])
6668
#print ("++++>cache.memory_used:", cache.memory_used)
6769
cache.put(CacheKey("a", [1, 'a', None]), [{"a1": 1}, {"a2": 2}, {"a3":3}])

0 commit comments

Comments
 (0)