Search in sources :

Example 1 with IMemcache

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();
    }
}
Also used : IMemcache(cn.cerc.jdb.cache.IMemcache) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with IMemcache

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;
    }
}
Also used : IStatus(cn.cerc.jbean.core.IStatus) IMemcache(cn.cerc.jdb.cache.IMemcache) Record(cn.cerc.jdb.core.Record) IService(cn.cerc.jbean.core.IService)

Example 3 with IMemcache

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);
}
Also used : IMemcache(cn.cerc.jdb.cache.IMemcache)

Example 4 with IMemcache

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() + " 缓存初始化!");
}
Also used : IMemcache(cn.cerc.jdb.cache.IMemcache) Type(java.lang.reflect.Type) SqlQuery(cn.cerc.jdb.mysql.SqlQuery) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with IMemcache

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);
    }
}
Also used : IMemcache(cn.cerc.jdb.cache.IMemcache) Gson(com.google.gson.Gson) Record(cn.cerc.jdb.core.Record) LocalService(cn.cerc.jbean.client.LocalService)

Aggregations

IMemcache (cn.cerc.jdb.cache.IMemcache)8 Record (cn.cerc.jdb.core.Record)3 SqlQuery (cn.cerc.jdb.mysql.SqlQuery)2 Gson (com.google.gson.Gson)2 LocalService (cn.cerc.jbean.client.LocalService)1 IService (cn.cerc.jbean.core.IService)1 IStatus (cn.cerc.jbean.core.IStatus)1 JPushRecord (cn.cerc.jmis.message.JPushRecord)1 MessageRecord (cn.cerc.jmis.message.MessageRecord)1 GsonBuilder (com.google.gson.GsonBuilder)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)1