高并发之Memcached实战第10课-“Memcached Get获取数据”部分代码分享2
一、Memcached客户端读写在同一个程序的逻辑:
MemcachedClient mcc = new MemcachedClient(list); if(mcc.get("something")==null) { if(!DataFactory.Exist(somethingObject)) { DataFactory.StoreInDB(somethingObject); } mcc.set("something", 900, somethingObject).get(); } ReadObject readObject =mcc.get("something"); readObject.DoClientLogic();
二、Memcached客户端读写分离的逻辑:
写逻辑:
MemcachedClient mcc = new MemcachedClient(list); if(mcc.get("something")==null) { if(!DataFactory.Exist(somethingObject)) { DataFactory.StoreInDB(somethingObject); } mcc.set("something", 900, somethingObject).get(); }
读逻辑:
MemcachedClient mcc = new MemcachedClient(list); ReadObject readObject =new ReadObject(); if(mcc.get("something")==null) { readObject =DataFactory.ReadFromDB(somethingObject); } else { readObject =mcc.get("something"); } readObject.DoClientLogic();
碰到任何问题,请回复,共同讨论,谢谢!
时间: 2024-10-08 19:35:16