use of cn.cerc.jdb.cache.IMemcache in project summer-bean by cn-cerc.
the class MemcachedTest method test.
// private static final Logger log = Logger.getLogger(MemcachedTest.class);
@Test
@Ignore
public void test() {
String buffKey = "test";
String value = "OK!";
IMemcache client = Application.getMemcache();
client.set(buffKey, value, 2);
Object buffData;
for (int i = 1; i < 5; i++) {
buffData = client.get(buffKey);
String msg = String.format("第 %d 次测试", i);
assertEquals(msg, i <= 2 ? value : null, buffData);
sleep();
}
}
use of cn.cerc.jdb.cache.IMemcache in project summer-bean by cn-cerc.
the class LocalService method exec.
// 带缓存调用服务
@Override
public boolean exec(Object... args) {
if (args.length > 0) {
Record headIn = getDataIn().getHead();
if (args.length % 2 != 0)
throw new RuntimeException("传入的参数数量必须为偶数!");
for (int i = 0; i < args.length; i = i + 2) headIn.setField(args[i].toString(), args[i + 1]);
}
if (handle == null)
throw new RuntimeException("handle is null.");
if (serviceCode == null)
throw new RuntimeException("service is null.");
IService bean = Application.getService(handle, serviceCode);
if (bean == null) {
this.message = String.format("bean %s not find", serviceCode);
return false;
}
if ((bean instanceof Microservice) && ((Microservice) bean).getService() == null)
((Microservice) bean).setService(serviceCode);
try {
if (!"AppSessionRestore.byUserCode".equals(this.serviceCode))
log.info(this.serviceCode);
if (ServerConfig.getAppLevel() == ServerConfig.appRelease) {
IStatus status = bean.execute(dataIn, dataOut);
boolean result = status.getResult();
message = status.getMessage();
return result;
}
IMemcache buff = Application.getMemcache();
// 制作临时缓存Key
String key = MD5.get(handle.getUserCode() + this.serviceCode + dataIn.getJSON());
if (bufferRead) {
String buffValue = (String) buff.get(key);
if (buffValue != null) {
log.debug("read from buffer: " + this.serviceCode);
dataOut.setJSON(buffValue);
message = dataOut.getHead().getString("_message_");
return dataOut.getHead().getBoolean("_result_");
}
}
// 没有缓存时,直接读取并存入缓存
bean.init(handle);
IStatus status = bean.execute(dataIn, dataOut);
boolean result = status.getResult();
message = status.getMessage();
if (bufferWrite) {
log.debug("write to buffer: " + this.serviceCode);
dataOut.getHead().setField("_message_", message);
dataOut.getHead().setField("_result_", result);
buff.set(key, dataOut.getJSON());
}
return result;
} catch (Exception e) {
Throwable err = e;
if (e.getCause() != null)
err = e.getCause();
log.error(err.getMessage(), err);
message = err.getMessage();
return false;
}
}
use of cn.cerc.jdb.cache.IMemcache in project summer-bean by cn-cerc.
the class UserList method clear.
@Override
public void clear() {
IMemcache cache = Application.getMemcache();
cache.delete(buffKey);
}
use of cn.cerc.jdb.cache.IMemcache in project summer-bean by cn-cerc.
the class UserList method init.
private void init() {
if (buff.size() > 0)
return;
// 从缓存中读取
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
IMemcache cache = Application.getMemcache();
String data = (String) cache.get(buffKey);
if (data != null && !"".equals(data)) {
Type type = new TypeToken<Map<String, UserRecord>>() {
}.getType();
Map<String, UserRecord> items = gson.fromJson(data, type);
for (String key : items.keySet()) {
buff.put(key, items.get(key));
}
log.debug(this.getClass().getName() + " 缓存成功!");
return;
}
// 从数据库中读取
SqlQuery ds = new SqlQuery(handle);
ds.add("select ID_,CorpNo_,Code_,Name_,QQ_,Mobile_,SuperUser_,");
ds.add("LastRemindDate_,EmailAddress_,RoleCode_,ProxyUsers_,Enabled_,DiyRole_ ");
ds.add("from %s ", SystemTable.get(SystemTable.getUserInfo));
ds.add("where CorpNo_='%s'", handle.getCorpNo());
ds.open();
while (ds.fetch()) {
String key = ds.getString("Code_");
UserRecord value = new UserRecord();
value.setId(ds.getString("ID_"));
value.setCorpNo(ds.getString("CorpNo_"));
value.setCode(ds.getString("Code_"));
value.setName(ds.getString("Name_"));
Map<String, Integer> priceValue = getPriceValue(ds.getString("Code_"));
value.setShowInUP(priceValue.get(UserOptions.ShowInUP));
value.setShowOutUP(priceValue.get(UserOptions.ShowOutUP));
value.setShowWholesaleUP(priceValue.get(UserOptions.ShowWholesaleUP));
value.setShowBottomUP(priceValue.get(UserOptions.ShowBottomUP));
value.setQq(ds.getString("QQ_"));
value.setMobile(ds.getString("Mobile_"));
value.setAdmin(ds.getBoolean("SuperUser_"));
value.setLastRemindDate(ds.getDateTime("LastRemindDate_").getDate());
value.setEmail(ds.getString("EmailAddress_"));
if (ds.getBoolean("DiyRole_"))
value.setRoleCode(ds.getString("Code_"));
else
value.setRoleCode(ds.getString("RoleCode_"));
value.setProxyUsers(ds.getString("ProxyUsers_"));
value.setEnabled(ds.getBoolean("Enabled_"));
buff.put(key, value);
}
// 存入到缓存中
cache.set(buffKey, gson.toJson(buff));
log.debug(this.getClass().getName() + " 缓存初始化!");
}
use of cn.cerc.jdb.cache.IMemcache in project summer-mis by cn-cerc.
the class MemoryBookInfo method get.
public static BookInfoRecord get(IHandle handle, String corpNo) {
IMemcache buff = Application.getMemcache();
String tmp = (String) buff.get(getBuffKey(corpNo));
if (tmp == null || "".equals(tmp)) {
LocalService svr = new LocalService(handle, "SvrBookInfo.getRecord");
if (!svr.exec("corpNo", corpNo))
return null;
BookInfoRecord result = new BookInfoRecord();
Record ds = svr.getDataOut().getHead();
result.setCode(ds.getString("corpNo"));
result.setShortName(ds.getString("shortName"));
result.setName(ds.getString("name"));
result.setAddress(ds.getString("address"));
result.setTel(ds.getString("tel"));
result.setManagerPhone(ds.getString("managerPhone"));
result.setStartHost(ds.getString("host"));
result.setContact(ds.getString("contact"));
result.setAuthentication(ds.getBoolean("authentication"));
result.setStatus(ds.getInt("status"));
result.setCorpType(ds.getInt("type"));
Gson gson = new Gson();
buff.set(getBuffKey(corpNo), gson.toJson(result));
return result;
} else {
Gson gson = new Gson();
return gson.fromJson(tmp, BookInfoRecord.class);
}
}
Aggregations