java - ConcurrentHashMap 作为缓存性能

标签 java spring performance caching join

我使用的是 spring 4,我为缓存创建了以下类以避免连接

package com.pu.services;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(BeanDefinition.SCOPE_SINGLETON)
public class CacheSRV {

private Map<Long, String> countriesMap = new ConcurrentHashMap<Long, String>();
private Map<Long, String> provinceMap = new ConcurrentHashMap<Long, String>();
private Map<Long, String> divisionMap = new ConcurrentHashMap<Long, String>();
private Map<Long, String> districtMap = new ConcurrentHashMap<Long, String>();
private Map<Long, String> cityMap = new ConcurrentHashMap<Long, String>();
private Map<Long, String> zoneMap = new ConcurrentHashMap<Long, String>();

public Map<Long, String> getCountriesMap() {
    return countriesMap;
}

public void setCountriesMap(Map<Long, String> countriesMap) {
    this.countriesMap = countriesMap;
}

public Map<Long, String> getProvinceMap() {
    return provinceMap;
}

public void setProvinceMap(Map<Long, String> provinceMap) {
    this.provinceMap = provinceMap;
}

public Map<Long, String> getDivisionMap() {
    return divisionMap;
}

public void setDivisionMap(Map<Long, String> divisionMap) {
    this.divisionMap = divisionMap;
}

public Map<Long, String> getDistrictMap() {
    return districtMap;
}

public void setDistrictMap(Map<Long, String> districtMap) {
    this.districtMap = districtMap;
}

public Map<Long, String> getCityMap() {
    return cityMap;
}

public void setCityMap(Map<Long, String> cityMap) {
    this.cityMap = cityMap;
}

public Map<Long, String> getZoneMap() {
    return zoneMap;
}

public void setZoneMap(Map<Long, String> zoneMap) {
    this.zoneMap = zoneMap;
}

}

当我获得值时,它会迭代所有映射以找出该值。 假设,我得到一个学生列表,学生列表大小为 100,世界上有 195 个国家。所以,这意味着每次迭代,我需要迭代 195 个国家才能找出属于学生的国家。这是性能开销吗?如果是,如何克服?

或者我最好使用 join

Select * from  Student ST
INNER JOIN COUNTRY C ON C.countryid = st.countryid
INNER JOIN Province P ON P.provinceid = st.provinceid 
INNER JOIN Division D ON D.divisionid = st.divisionid 
INNER JOIN District DS ON DS.districtid = st.districtid 
INNER JOIN City CT ON CT.cityid = st.cityid 
INNER JOIN Zone ZE ON ZE.zoneid = st.zoneid

最佳答案

there are 195 countries in the world. so , its means for every iteration , I need to iterate 195 countries to find out the country belong to student.

什么?您不想使用 map 吗?

countriesMap.get(student.countryId)

你就完成了。这是一个恒定时间操作(除了罕见的过度碰撞问题)。

虽然数据库非常擅长连接,但使用 map 肯定会更快。然而,它还有其他缺点:

  • 如果数据发生更改,您需要与表格保持同步。这可能会变得相当复杂。
  • 您的 map 受到物理内存的限制,而您的表格可能会大几个数量级。

对于您的情况来说,两者都应该没有问题。是否值得这样做的问题仍然存在。

关于java - ConcurrentHashMap 作为缓存性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47404584/

相关文章:

java - 将 XML 属性限制为枚举值

java - 如何在 web.xml 中添加应用程序上下文

java - (Spring Boot 2)java.lang.IllegalStateException : Failed to load ApplicationContex

spring - Run As : Spring Boot App and Run As: Java Application? 之间有区别吗

java - 为什么将第一项添加到集合中比第二项慢得多?

c++ - 简单的 getter/accessor 防止矢量化 - gcc 错误?

java - 无法从Spring云配置服务器中的GIT读取属性源

java - 泛型和 compareTo() 方法

performance - HDFS序列文件性能调优

java - 从另一个类 JFrame 调用重绘