In memory cache in twisted

Waqar Khan <[email protected]> Thu, 26 Sep 2019 22:38:46 -0400
Newsgroups gmane.comp.python.twisted
Message-ID <CAJuJkHPF7RMbShtxJwg_Z4t+S0eEJWgy6FWOb0hhyae0K-ic=A@mail.gmail.com>
--===============5462300088871230766==
Content-Type: multipart/alternative; boundary="000000000000b4591305937fc978"

--000000000000b4591305937fc978
Content-Type: text/plain; charset="UTF-8"

Hi,
  What's a good way to use a simple dictionary as a cache in twisted
framework?
Basically, I have this callback chain where I ultimately make a rest call
(in non-blocking way using treq) to fetch some data. But before I make the
call, I am using a dictionary to see if the value is available or not.
But, I have noticed that the event loop gets pretty busy(sometimes, things
get stuck and twisted server stops) as soon as I add this logic.. Which is
pretty much

@defer.inlinecallbacks
def fetch(key):
      if key in cache:
               return cache[key]
      # else call back to treq to fetch value
       cache[key] = value
       return value

This dict can grow to around 50k.. What's a good way to solve this issue?

--000000000000b4591305937fc978
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div>=C2=A0 What&#39;s a good way to use a simple dicti=
onary as a cache in twisted framework?=C2=A0</div><div>Basically, I have th=
is callback chain where I ultimately make a rest call (in non-blocking way =
using treq) to fetch some data. But before I make the call, I am using a di=
ctionary to see if the value is available or not.</div><div>But, I have not=
iced that the event loop gets pretty busy(sometimes, things get stuck and t=
wisted server stops) as soon as I add this logic.. Which is pretty=C2=A0muc=
h</div><div><br></div><div>@defer.inlinecallbacks<br></div><div>def fetch(k=
ey):</div><div>=C2=A0 =C2=A0 =C2=A0 if key in cache:</div><div>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return cache[key]</div><div>=
=C2=A0 =C2=A0 =C2=A0 # else call back to treq to fetch value</div><div>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0cache[key] =3D value</div><div>=C2=A0 =C2=A0 =C2=A0=
 =C2=A0return value</div><div><br></div><div>This dict can grow to around 5=
0k.. What&#39;s a good way to solve this issue?</div></div>

--000000000000b4591305937fc978--

--===============5462300088871230766==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KVHdpc3RlZC1Q
eXRob24gbWFpbGluZyBsaXN0ClR3aXN0ZWQtUHl0aG9uQHR3aXN0ZWRtYXRyaXguY29tCmh0dHBz
Oi8vdHdpc3RlZG1hdHJpeC5jb20vY2dpLWJpbi9tYWlsbWFuL2xpc3RpbmZvL3R3aXN0ZWQtcHl0
aG9uCg==

--===============5462300088871230766==--