การแคชไม่จำเป็นต้องยากอีกต่อไป ด้วย รหัสแคช Omoide เพียงไม่กี่บรรทัดจะนำบริการ Python ของคุณไปอีกระดับ!
Library ที่ปล่อยออกมา : PYPI
ซอร์สโค้ด : GitHub
การสอน№1 : บล็อกหรือสื่อของฉัน
นี่คือโซลูชันแคชในหน่วยความจำที่แข็งแกร่งสามารถปรับได้สูง
มันถูกออกแบบมาให้เป็นแคชระดับวิธีการล้อมรอบวิธีการเรียนเดียวโดยใช้อาร์กิวเมนต์การโทรวิธีเป็นคีย์แคชและจัดเก็บค่าคืนสินค้า
ปรับแต่งให้เหมาะกับกรณีการใช้งานเฉพาะของคุณมีตัวเลือกการหมดอายุและการรีเฟรชที่หลากหลาย
ใช้งานง่ายมากและง่ายต่อการรวมเข้ากับมัณฑนากรที่เรียบง่าย (เช่นคำอธิบายประกอบสำหรับผู้ที่มาจาก Java) ไม่จำเป็นต้องเพิ่มตรรกะที่ซับซ้อนลงในรหัสของคุณเพียงใช้ @omoide_cache() ด้านบนของวิธีการใด ๆ ในบริการของคุณ มันจะสร้างแคชโดยอัตโนมัติสำหรับวิธีการของคุณด้วยการตั้งค่าเริ่มต้น คุณสามารถปรับการตั้งค่าเหล่านี้เพิ่มเติมผ่านพารามิเตอร์มัณฑนากร
คำเตือนอย่างยุติธรรม - โครงการนี้อยู่ในช่วงแรกของวงจรชีวิตจะมีการปรับปรุงและแก้ไขข้อผิดพลาดมากมายในอนาคต คำแนะนำและรายงานข้อผิดพลาดทั้งหมดยินดีต้อนรับอย่างมาก!
pip install omoide-cachegit clone https://github.com/jpleorx/omoide-cache.git
cd omoide-cache
pip install --editable . import time
from omoide_cache import omoide_cache
# A class where cache was added to a simulated long running method
class ExampleService :
@ omoide_cache ()
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x
service = ExampleService ()
# The first call will execute real logic and store the result in cache
service . time_consuming_method ( 1 )
# The second call will get results from cache
service . time_consuming_method ( 1 )ที่นี่เราเพิ่มแคชที่จะวางรายการที่เข้าถึงได้บ่อยครั้งน้อยที่สุดเมื่อแคชมีขนาดใหญ่เกินไป
import time
from omoide_cache import omoide_cache , ExpireMode
class ExampleService :
@ omoide_cache ( max_allowed_size = 10 , size_expire_mode = ExpireMode . ACCESS_COUNT_BASED )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x ที่นี่แคชจะลบรายการที่เข้าถึงได้ล่าสุดโดยอัตโนมัติมากกว่า 2 นาทีที่ผ่านมา
import time
from omoide_cache import omoide_cache
class ExampleService :
@ omoide_cache ( expire_by_access_duration_s = 120 )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * xหรือเราสามารถลบรายการที่คำนวณได้มากกว่า 2 นาทีที่ผ่านมา
import time
from omoide_cache import omoide_cache
class ExampleService :
@ omoide_cache ( expire_by_computed_duration_s = 120 )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x ที่นี่แคชจะรีเฟรชรายการแบบอะซิงโครนัสที่คำนวณได้มากกว่า 2 นาทีที่ผ่านมา ความพยายามที่จะรีเฟรชจะดำเนินการทุก 10 วินาที
import time
from omoide_cache import omoide_cache , RefreshMode
class ExampleService :
@ omoide_cache ( refresh_duration_s = 120 , refresh_period_s = 10 , refresh_mode = RefreshMode . INDEPENDENT )
def time_consuming_method ( self , x : int ) -> int :
time . sleep ( 2.0 )
return x * x@omoide_cache() แต่ไม่ใช่ @omoide_cache ฉันไม่มีความคิดร่วมเพศว่าทำไมพฤติกรรมแปลก ๆ นี้ในนักตกแต่งจะทำให้ดีที่สุดเพื่อแก้ไขในการอัปเดตในอนาคตในกรณีที่คุณต้องการตรวจสอบงานอื่นหรือติดต่อฉัน: