private V solrGet(K key) throws Exception { String id = buildSolrId(key); JsonObject doc = null; Exception ex = null; for (int i = 0; i < _urlGets.size(); i++) { try { doc = SolrTools.getDoc(getSolrGetUrl(), _connectTimeout, _readTimeout, id); ex = null; break; } catch (Exception e) { ex = e; try { Thread.sleep(100); } catch (InterruptedException e1) { } } } if (ex != null) { throw ex; } if (doc == null) { return null; } if (_mapName.startsWith(MEMCACHED_PREFIX)) { // 判断memcache是否超期 Date birthday = SolrTools.solrDateFormat.parse(doc.getString(SolrTools.F_HZ_CTIME)); if ((System.currentTimeMillis() - birthday.getTime()) >= DAY_30) { // 超期30天 solrDelete(key); return null; } } String sClass = doc.getString(SolrTools.F_HZ_CLASS); String sValue = doc.getString(SolrTools.F_HZ_DATA); return (V) JsonObject.fromJson(sValue, Class.forName(sClass)); }
@Override public Set<K> loadAllKeys() { if (_loadAll == false) { return null; } if (_hazelcastInstance.getCluster().getMembers().size() > 1) { return null; } Set<K> set = new HashSet<K>(); try { boolean stop = false; int startIndex = 0; int prfexPos = (_mapName + ":").length(); JsonObject solrResponse = null; while (!stop) { if (startIndex == 0) { solrResponse = solrSelect(startIndex, "*"); } else { solrResponse = solrSelect(startIndex, solrResponse.getString("nextCursorMark")); } JsonArray docs = solrResponse.getObject("response").getArray("docs"); if (docs.size() == 0) { break; } JsonObject doc; K key; for (int i = 0; i < docs.size(); i++) { doc = docs.get(i); JsonObject jsonKey = new JsonObject(doc.getString(SolrTools.F_ID).substring(prfexPos)); if (jsonKey.getString("C").equalsIgnoreCase("S")) { key = (K) jsonKey.getString("V"); } else { key = (K) JsonObject.fromJson( jsonKey.getString("V"), Class.forName(jsonKey.getString("C"))); } if (_mapName.startsWith(MEMCACHED_PREFIX)) { // 判断memcache是否超期 Date birthday = SolrTools.solrDateFormat.parse(doc.getString(SolrTools.F_HZ_CTIME)); if ((System.currentTimeMillis() - birthday.getTime()) >= DAY_30) { // 超期30天 solrDelete(key); continue; } } set.add(key); } _logger.log( Level.INFO, "loadAllKeys():" + _mapName + ":size:" + set.size() + ":startIndex:" + startIndex); startIndex = startIndex + docs.size(); int numFound = solrResponse.getObject("response").getInteger("numFound"); if (startIndex >= numFound) { break; } } if (set.size() == 0) { return null; } else { _logger.log( Level.INFO, "Final loadAllKeys():" + _mapName + ":size:" + set.size() + ":startIndex:" + startIndex); return set; } } catch (Exception e) { _logger.log(Level.SEVERE, e.getMessage(), e); throw new RuntimeException(e); } }