Kekik 1.6.3__py3-none-any.whl → 1.6.5__py3-none-any.whl

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.

Potentially problematic release.


This version of Kekik might be problematic. Click here for more details.

Kekik/cache.py CHANGED
@@ -11,7 +11,7 @@ UNLIMITED = None
11
11
  class Cache:
12
12
  """
13
13
  Basit in-memory cache yapısı.
14
- TTL (time-to-live) süresi dolan veriler otomatik olarak temizlenir.
14
+ TTL (time-to-live) süresi dolan veriler otomatik olarak temizlenir (get/erişim anında kontrol edilir).
15
15
  """
16
16
  def __init__(self, ttl=UNLIMITED):
17
17
  self._data = {}
@@ -49,11 +49,34 @@ class AsyncCache(Cache):
49
49
  """
50
50
  Asenkron işlemleri destekleyen cache yapısı.
51
51
  Aynı key için gelen eşzamanlı çağrılar, futures kullanılarak tek sonuç üzerinden paylaşılır.
52
+ Ek olarak, belirli aralıklarla cache’i kontrol edip, süresi dolmuş verileri temizleyen otomatik temizleme görevi çalışır.
52
53
  """
53
- def __init__(self, ttl=UNLIMITED):
54
+ def __init__(self, ttl=UNLIMITED, cleanup_interval=60 * 60):
55
+ """
56
+ :param ttl: Her entry için geçerli süre (saniye). Örneğin 3600 saniye 1 saattir.
57
+ :param cleanup_interval: Otomatik temizleme görevinin kaç saniyede bir çalışacağını belirler.
58
+ """
54
59
  super().__init__(ttl)
55
60
  self.futures = {}
56
61
 
62
+ if ttl is not UNLIMITED:
63
+ # TTL geçerli bir sayı olduğunda cleanup interval, ttl ile cleanup_interval'ın max'ı olarak ayarlanır.
64
+ self._cleanup_interval = max(ttl, cleanup_interval)
65
+ else:
66
+ # TTL tanımsız (UNLIMITED) ise cleanup_interval doğrudan kullanılır.
67
+ self._cleanup_interval = cleanup_interval
68
+
69
+ self._cleanup_task = asyncio.create_task(self._auto_cleanup())
70
+
71
+ async def _auto_cleanup(self):
72
+ """Belirlenen aralıklarla cache içerisindeki süresi dolmuş entry'leri temizler."""
73
+ while True:
74
+ await asyncio.sleep(self._cleanup_interval)
75
+ # _data kopyasını almak, üzerinde dönüp silme yaparken hata almamak için.
76
+ keys = list(self._data.keys())
77
+ for key in keys:
78
+ self.remove_if_expired(key)
79
+
57
80
  async def get(self, key):
58
81
  """
59
82
  Belirtilen key için cache'de saklanan değeri asenkron olarak döndürür.
@@ -89,29 +112,38 @@ def _sync_maybe_cache(func, key, result, unless):
89
112
 
90
113
  async def _async_compute_and_cache(func, key, unless, *args, **kwargs):
91
114
  """
92
- Asenkron fonksiyon sonucu hesaplandıktan sonra, sonucu cachee ekler.
93
- Eğer `unless` koşulu sağlanıyorsa cache kaydı atlanır.
115
+ Asenkron fonksiyon sonucunu hesaplar ve cache'e ekler.
116
+ Aynı key için işlem devam ediyorsa, mevcut sonucu bekler.
117
+ Sonuç, unless(result) True değilse cache'e eklenir.
94
118
  """
95
- cache = func.__cache
119
+ # __cache'den cache nesnesini alıyoruz.
120
+ cache = func.__cache
121
+
122
+ # Aynı key için aktif bir future varsa, onun sonucunu döndür.
123
+ if key in cache.futures:
124
+ return await cache.futures[key]
125
+
126
+ # Yeni future oluşturuluyor ve cache.futures'e ekleniyor.
96
127
  future = asyncio.Future()
97
128
  cache.futures[key] = future
98
129
 
99
130
  try:
131
+ # Asenkron fonksiyonu çalıştır ve sonucu elde et.
100
132
  result = await func(*args, **kwargs)
133
+ future.set_result(result)
134
+
135
+ # unless koşuluna göre cache'e ekleme yap.
136
+ if unless is None or not unless(result):
137
+ await cache.set(key, result)
138
+
139
+ return result
101
140
  except Exception as exc:
102
- cache.futures.pop(key, None)
103
141
  future.cancel()
104
142
  raise exc
105
-
106
- future.set_result(result)
107
-
108
- if unless is None or not unless(result):
109
- cache[key] = result
110
- else:
143
+ finally:
144
+ # İşlem tamamlandığında future'ı temizle.
111
145
  cache.futures.pop(key, None)
112
146
 
113
- return result
114
-
115
147
  async def make_cache_key(args, kwargs, is_fastapi=False):
116
148
  """
117
149
  Cache key'ini oluşturur.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: Kekik
3
- Version: 1.6.3
3
+ Version: 1.6.5
4
4
  Summary: İşlerimizi kolaylaştıracak fonksiyonların el altında durduğu kütüphane..
5
5
  Home-page: https://github.com/keyiflerolsun/Kekik
6
6
  Author: keyiflerolsun
@@ -2,7 +2,7 @@ Kekik/BIST.py,sha256=gFvEFdLGgsxUq33AkqOr5pVGsSm9jR0v6DVwoSzhX7w,1375
2
2
  Kekik/Domain2IP.py,sha256=dMDatPElTMfLORzC_QN9znex8Se1eid3EPsxAe5S1aQ,4851
3
3
  Kekik/Nesne.py,sha256=33pp49cZkRCoa0aUFfzVMpx5FSJchSdiKhm97Nkol1Q,7020
4
4
  Kekik/__init__.py,sha256=Iy-O4c_DuY2ttQJv6j7xoVk3IS9EcC0Aqx3vhFvCgd8,1057
5
- Kekik/cache.py,sha256=Dty5JgcU5Ufh4qoP-fktBSD6usF4B5hg6U5X7AqoPqA,6210
5
+ Kekik/cache.py,sha256=N89SxrbsvSzzCDF5cMwYPp_2qtW4Ii-fvGya5_E0yZw,7950
6
6
  Kekik/cli.py,sha256=I--MV8-okBSUYAaV4fRsMeB2n57OJITrgbEnK1OdTPY,3648
7
7
  Kekik/csv2dict.py,sha256=HO0AgREXE0yM7cL4OwqpkCGm2pz31RTb0mhnTg6gdwo,423
8
8
  Kekik/dict2csv.py,sha256=AcGvEg9i5MXtwMwg7WiDxOj8h9LldNjjcRiWFwA9XJU,560
@@ -34,9 +34,9 @@ Kekik/kisi_ver/__init__.py,sha256=gH613YZC3ziE7f67dViefRSuwDwsHyVvXHpM5CzZ2q4,13
34
34
  Kekik/kisi_ver/biyografiler.py,sha256=5Xv1ixaDGHGtl5Nf92jo9dPgF3jDXOGEPmWgoEsn9j8,189486
35
35
  Kekik/kisi_ver/isimler.py,sha256=zHVimWL_4TvoLE3qzWQslDBc8-IJZSB02s0vRwsVM1g,88066
36
36
  Kekik/kisi_ver/soyisimler.py,sha256=YQJYp2SjENgwOaCa9mmShxPYeeUll2cq8Vox-d8_kB8,78485
37
- Kekik-1.6.3.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
38
- Kekik-1.6.3.dist-info/METADATA,sha256=CUgNBONKGqwdYzT5RhF8fE1qmnZTtSv3y3bEyfjqw8c,43959
39
- Kekik-1.6.3.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
40
- Kekik-1.6.3.dist-info/entry_points.txt,sha256=yjBifxtRlqfg8lPkH4Bu-urSa5ecptCHsuth-DcyWcg,59
41
- Kekik-1.6.3.dist-info/top_level.txt,sha256=NotddscfgxawvuRyAa7xkgnMhyteFDcBxb5aU5GY3BM,6
42
- Kekik-1.6.3.dist-info/RECORD,,
37
+ Kekik-1.6.5.dist-info/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
38
+ Kekik-1.6.5.dist-info/METADATA,sha256=2EDjr2aLS_JbWgpD_P17bm7BiqPSzuHrMYUX_EVn4yc,43959
39
+ Kekik-1.6.5.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
40
+ Kekik-1.6.5.dist-info/entry_points.txt,sha256=yjBifxtRlqfg8lPkH4Bu-urSa5ecptCHsuth-DcyWcg,59
41
+ Kekik-1.6.5.dist-info/top_level.txt,sha256=NotddscfgxawvuRyAa7xkgnMhyteFDcBxb5aU5GY3BM,6
42
+ Kekik-1.6.5.dist-info/RECORD,,
File without changes
File without changes