对于我们的java项目,我们使用hazelcast服务器和客户端模型在加载应用程序时缓存数据库中的数据。这是我的hazelcast.xml
<hazelcast>
<group>
<name>dev</name>
<password>dev-pass</password>
</group>
<management-center enabled="true">http://localhost:8080/mancenter</management-center>
<network>
<port auto-increment="true">5701</port>
<join>
<multicast enabled="false"></multicast>
<tcp-ip enabled="true">
<member>172.22.3.74</member>
<!--<interface>127.0.0.1</interface>-->
</tcp-ip> </join>
<interfaces enabled="false">
<interface>10.10.1.*</interface>
</interfaces>
<symmetric-encryption enabled="false">
<algorithm>PBEWithMD5AndDES</algorithm>
<!-- salt value to use when generating the secret key -->
<salt>thesalt</salt>
<!-- pass phrase to use when generating the secret key -->
<password>thepass</password>
<!-- iteration count to use when generating the secret key -->
<iteration-count>19</iteration-count>
</symmetric-encryption>
</network>
<properties>
<property name="hazelcast.http.healthcheck.enabled">true</property>
<property name="hazelcast.health.monitoring.delay.seconds">5</property>
<property name="hazelcast.health.monitoring.level">NOISY</property>
</properties>
<map name="scripMasterMap">
<near-cache name="default">
<in-memory-format>BINARY</in-memory-format>
<time-to-live-seconds>300</time-to-live-seconds>
<max-idle-seconds>100</max-idle-seconds>
</near-cache>
<backup-count>1</backup-count>
<time-to-live-seconds>8000</time-to-live-seconds>
<max-idle-seconds>3000</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<eviction-percentage>25</eviction-percentage>
<merge-policy>hz.ADD_NEW_ENTRY</merge-policy>
</map>
以下是hazelcast服务器代码。
ref data.Java:
HazelcastInstance in = Hazelcast.newHazelcastInstance();
IMap<Integer, ScripMaster> map = in.getMap("scripMasterMap");
for(ScripMaster scripMaster: scripMasterService.getAllScripMasterList()) {
scripMasterMap.put(scripMaster.getAllToken(), scripMaster);
}
以下是hazelcast客户端代码:
ClientConfig cl = new ClientConfig();
HazelcastInstance client = Hazelcast.newHazelcastClient(cl);
IMap<Object, Object> map = cl.getMap("scripMasterMap");
return map;
下面是我的方法,它从hazelcast客户端地图返回数据。
public Set<String> getAssetByInstrumentName(
IMap<Integer, ScripMaster> map, String instrument) {
Set<String> myset = new HashSet<String>();
Predicate namePredicate = Predicates.equal("instrument", instrument);
Collection<ScripMaster> assets = map.values(namePredicate );
for(Iterator<ScripMaster> it = assets.iterator(); it.hasNext();) {
ScripMaster scripmaster = it.next();
myset.put(scripmaster.getAsset());
}
return myset;
}
这是简单的代码。是否有任何遗漏需要做的事情,以便客户可以从内存中获取数据?这部分现在解决了。
public Set<String> getAllCommodity(IMap<Integer, ScripMaster> map) {
Set<String> commoditySet = new HashSet<String>();
Aggregator<Map.Entry<Integer, ScripMaster>, Set<String>> aggregation = new CommodityAggregator();
PropertyExtractor<ScripMaster, String> propertyExtractor = new CommodityPropertyExtractor();
commoditySet = map.aggregate(aggregation);
// Aggregators.distinct("scripMaster.commodity");
// Aggregators.distinct();
LOG.info("Number of commodities loaded::" + commoditySet.size());
return commoditySet;
}
public class CommodityAggregator extends Aggregator <Map.Entry<Integer, ScripMaster>, Set<String>> {
Set<String> commoditySet = new HashSet<String>();
@Override
public void accumulate(Map.Entry<Integer, ScripMaster> input) {
commoditySet.add(input.getValue().getCommodity());
}
@Override
public void combine(Aggregator aggregator) {
}
@Override
public Set<String> aggregate() {
return commoditySet;
}
}
在仪器字段上添加索引。