c# - Redis - Get single element by some "key" -
my team , have c# e-commerce engine. on website used memory cache, cache migrated redis.
we have web application in azure hosted cache in azure redis too.
whenever needed collection of cache use smembers
command.
take example collection of products. have 1,000 items in cache collection. every time have access product, need run smembers
command , cache collection, takes 250 milliseconds. problem design of application need run maybe 5 times on single page, increases load times.
example:
public product getproductbyid(int productid) { product product = null; try { list<product> products = getallproducts(); if (products != null && products.count > 0) product = products.find((p) => p.productid == productid); } catch (exception ex) { console.writeline(ex.tostring()); } return product; } public list<product> getallproducts() { return getcollectionfromset<product>(products_all_key); } /// <summary> /// smembers key /// </summary> /// <returns>array reply: elements of set. /// <remarks>http://redis.io/commands/smembers</remarks> public list<tentity> getcollectionfromset<tentity>(string urn) { try { list<tentity> list = new list<tentity>(); idatabase cache = connection.getdatabase(); redisvalue[] values2 = cache.setmembers(urn); foreach (redisvalue value in values2) { var entityinbit = deserialize<tentity>(value); list.add(entityinbit); } return list; } catch (exception ex) { console.writeline(ex.tostring()); return null; } }
stackoverflow miniprofiler example
the big question is: there way single element without bringing entire collection , filter through c# linq?
the short answer "no". you're serializing data format redis knows nothing cannot apply filtering you.
in order more efficient lookup need change way store data, example, if each of products stored standard key can direct o(1) lookup fetch - , if know ids front can pipeline this.
Comments
Post a Comment