ConcurrentHashMap实现缓存池

Map实现缓存池介绍

缓存池是基于map实现的一种本机缓存的解决方案,优点就是不会重复创建对象(复用性),避免浪费资源和Gc回收造成的开销和查询Map复用对象比较快,缺点也显而易见因为他是通过Map来存储该复用对象,所以受限于map的值不易被Gc回收,一直保存到内存中,如果使用map来存储大量复用的数据,一直不能清除的话会导致内存泄漏。

-ps 使用基于Map缓存池要考虑业务场景,如果是复用对象是有边界则可以使用(如:文件存储对象,消息队列名等),如果是无边界的不推荐使用(如:用户对象,商品对象等)

这个案例我使用RabbitMQ的主题作为唯一标识

package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 	$RabbitTemplateContainer池化封装
 * 	每一个topic 对应一个RabbitTemplate
 *	1.	提高发送的效率
 * 	2. 	可以根据不同的需求制定化不同的RabbitTemplate, 比如每一个topic 都有自己的routingKey规则
 * @author duanlinpeng
 */
@Slf4j
@Component
class RabbitTemplateContainer {
    /* key :TOPIC , value : Template*/
    private Map<String, RabbitTemplate> rabbitMap = new ConcurrentHashMap();

    @Autowired
    private ConnectionFactory connectionFactory;

    /**
     * @param message 队列消息对象
     * @return
     * @throws MessageRunTimeException
     */
    public RabbitTemplate getTemplate(Message message) throws MessageRunTimeException {
        //校验是否为空
        Preconditions.checkNotNull(message);
        //获取主题名
        String topic = message.getTopic();
        RabbitTemplate rabbitTemplate = rabbitMap.get(topic);
        //返回池中已经存在对象
        if (rabbitTemplate != null) {
            return rabbitTemplate;
        }
        log.info("RabbitTemplateContainer.getTemplate topic: {} 不存在,创建一个", topic);

        RabbitTemplate newTemplate = new RabbitTemplate(connectionFactory);
        newTemplate.setExchange(topic);
        newTemplate.setRoutingKey(message.getRoutingKey());
        newTemplate.setRetryTemplate(new RetryTemplate());

        rabbitMap.put(topic, newTemplate);

        return rabbitMap.get(topic);

    }
}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值