caching - High CPU usage after using Redis cache in c# async -
i using https://github.com/stackexchange/stackexchange.redis. (stackexchange.redis.strongname.1.2.1)
i have async function causes cpu 100% , start getting timeout error after 4-5 min after serving 400 requests
public async task<t> getorsetasync<t>(string cachekey, func<task<t>> getitemcallback) t : class { t item = null; idatabase cache = connection.getdatabase(); var cachevalue = await cache.stringgetasync(cachekey); if (cachevalue.isnull) { item = await getitemcallback(); await cache.stringsetasync(cachekey, jsonconvert.serializeobject(item)); } else { item = await task.factory.startnew(() => jsonconvert.deserializeobject<t>(cachevalue)); } return item; } if stop using redis cache , return direct values db, able execute load of 1300 request in 2 min 20 sec. cpu high still able complete load.
public async task<t> getorsetasync<t>(string cachekey, func<task<t>> getitemcallback) t : class { return await getitemcallback(); } if modify function below getdatabase , nothing. cause cpu goto 100% , stuck after 200 requests in 2 min , because of high cpu.
public async task<t> getorsetasync<t>(string cachekey, func<task<t>> getitemcallback) t : class { idatabase cache = connection.getdatabase(); return await getitemcallback(); } but question why cpu usage increased addition of
idatabase cache = connection.getdatabase(); ?
how "connection" property implemented? creating new connection redis on each call? if so, not recommended. should share single connection across calls.
private static lazy<connectionmultiplexer> lazyconnection = new lazy<connectionmultiplexer>(() => { return connectionmultiplexer.connect("<your connection string here>"); }); public static connectionmultiplexer connection { { return lazyconnection.value; } }
Comments
Post a Comment