cassandra外部使用示例三

续二的类:
    /**
     * 从主页面点击产品定位到导航页面
     */
    public List<Map<String, Object>> getThinkwordByDocFamily(Map map)
        throws ApplicationException
    {
        String nlsLang = (String) map.get("nlsLang");
        String itemName = (String) map.get("itemName");
        String itemId = (String) map.get("itemId");
        String aKeyArea = CACHE_FUNCTION + ".getThinkwordByDocFamilyIndex";
        String aName = nlsLang + ":"+ itemId + itemName;
        //通过 aKeyArea 和 aName 获取缓存中的对象
        if (IS_USE_CACHE)
        {
            List cacheList = (List) getLivingCacheObject(aKeyArea, aName);
            if (cacheList != null && cacheList.size() > 0)
            {
                return cacheList;
            }
        }
        //从数据库取数据,再存入缓存;
        List dataList = navigationDao.getThinkwordByDocFamily(map);
        if (IS_USE_CACHE && dataList != null && dataList.size() > 0)
        {
            pushCacheObject(aKeyArea, aName, dataList);
        }
        return dataList;
    }

    /**通过ID获取HtmlArea静态脚本
     */
    public Map<String, Object> getHtmlArea(String ID)
        throws ApplicationException
    {
        String aKeyArea = CACHE_FUNCTION + ".getHtmlArea";
        String aName = ID;

        //通过 aKeyArea 和 aName 获取缓存中的对象
        if (IS_USE_CACHE)
        {
            Map cacheMap = (Map) getLivingCacheObject(aKeyArea, aName);
            if (cacheMap != null && cacheMap.size() > 0)
            {
                return cacheMap;
            }
        }
        //从数据库取数据,再存入缓存;
        Map dataMap = navigationDao.getHTMLArea(ID);
        if (IS_USE_CACHE && dataMap != null && dataMap.size() > 0)
        {
            pushCacheObject(aKeyArea, aName, dataMap);
        }
        return dataMap;
    }

    /**获取缓存生命周期有效的缓存对象
     */
    private Object getLivingCacheObject(String aKeyArea, String aName)
        throws ApplicationException
    {
        Map cacheMap;
        try
        {
            cacheMap = cache.getResultMap(aKeyArea, aName);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new ApplicationException("cache getResultMap error", e);
        }
        if (cacheMap != null)
        {
            long pushTime = (Long) cacheMap.get("cache.timestamp");
            long liveTime = System.currentTimeMillis() - pushTime;
            Object cacheObject = cacheMap.get("cache.value");
            if (liveTime < CACHE_LIFE_CYCLE)
            {
                sLog.info(" getLivingCacheObject : --aKeyArea=" + aKeyArea
                        + "; --aName=" + aName + "; ");
                return cacheObject;
            }
        }
        return null;
    }

    /** 缓存一个可序列化的数据对象
     */
    private void pushCacheObject(String aKeyArea, String aName, Object object)
        throws ApplicationException
    {
        try
        {
            sLog.info(" pushCacheObject :  --aKeyArea=" + aKeyArea
                    + "; --aName=" + aName + "; ");
            cache.put(aKeyArea, aName, (Serializable) object);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new ApplicationException("cache put error", e);
        }
    }
}

你可能感兴趣的:(cache,脚本,cassandra)