/** * Find Record by cache. * * @see #find(String, Object...) * @param cacheName the cache name * @param key the key used to get date from cache * @return the list of Record */ public List<Record> findByCache(String cacheName, Object key, String sql, Object... paras) { ICache cache = config.getCache(); List<Record> result = cache.get(cacheName, key); if (result == null) { result = find(sql, paras); cache.put(cacheName, key, result); } return result; }
/** * Find first record by cache. I recommend add "limit 1" in your sql. * * @see #findFirst(String, Object...) * @param cacheName the cache name * @param key the key used to get date from cache * @param sql an SQL statement that may contain one or more '?' IN parameter placeholders * @param paras the parameters of sql * @return the Record object */ public Record findFirstByCache(String cacheName, Object key, String sql, Object... paras) { ICache cache = config.getCache(); Record result = cache.get(cacheName, key); if (result == null) { result = findFirst(sql, paras); cache.put(cacheName, key, result); } return result; }
public static void main(String[] args) throws Exception { Config config = new Config(); Cache cache = config.getCache(); // cache data generator sequence FixedSizeElementSequence elementSequence = new FixedSizeElementSequence(Config.NUMBER_OF_ENTRIES, Config.SIZE_OF_ENTRY); cache.removeAll(); LOG.info("cache size should be zero. size=" + cache.getSize() + "\n starting load"); Util.sleepFor(3); cache.setNodeBulkLoadEnabled(true); long start = System.currentTimeMillis(); // single threaded for now // start putting data into the cache PutWorker putWorker = new PutWorker(cache, elementSequence); // TODO:use executor to make this multi-threaded putWorker.start(); // for now, waiting for put to complete putWorker.join(); // now remove bulkmode long endOfWorkerThread = System.currentTimeMillis(); cache.setNodeBulkLoadEnabled(false); Config.CALL_TIMER.stop(); // stop the timer long endOfBulkLoadReset = System.currentTimeMillis(); int endSize = cache.getSize(); LOG.info( "complete. total time for " + Config.NUMBER_OF_ENTRIES + " puts to complete is " + (endOfWorkerThread - start) + "ms, and time it took to reset bulk mode is " + (endOfBulkLoadReset - endOfWorkerThread) + "ms \n End size=" + endSize); // just wait for return // for now, waiting for user input to close the // program. This gives us time to look at the metrics on JMX console or // TMC :) Util.waitForInput(); ConsoleReporter.enable(1, TimeUnit.SECONDS); Util.sleepFor(2); }
/** * Paginate by cache. * * @see #paginate(int, int, String, String, Object...) * @return Page */ public Page<Record> paginateByCache( String cacheName, Object key, int pageNumber, int pageSize, String select, String sqlExceptSelect, Object... paras) { ICache cache = config.getCache(); Page<Record> result = cache.get(cacheName, key); if (result == null) { result = paginate(pageNumber, pageSize, select, sqlExceptSelect, paras); cache.put(cacheName, key, result); } return result; }