NetworkAddressCacheCaffeineService.java 3.8 KB
Newer Older
彭勇升 pengys 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.skywalking.apm.collector.cache.caffeine.service;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.apache.skywalking.apm.collector.cache.service.NetworkAddressCacheService;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.core.util.StringUtils;
import org.apache.skywalking.apm.collector.storage.StorageModule;
import org.apache.skywalking.apm.collector.storage.dao.cache.INetworkAddressCacheDAO;
import org.apache.skywalking.apm.collector.storage.table.register.NetworkAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

32 33 34 35
import java.util.concurrent.TimeUnit;

import static java.util.Objects.isNull;

彭勇升 pengys 已提交
36 37 38 39 40 41 42
/**
 * @author peng-yongsheng
 */
public class NetworkAddressCacheCaffeineService implements NetworkAddressCacheService {

    private final Logger logger = LoggerFactory.getLogger(NetworkAddressCacheCaffeineService.class);

43
    private final Cache<String, Integer> addressCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(1000).maximumSize(5000).build();
彭勇升 pengys 已提交
44 45 46 47 48 49 50 51 52

    private final ModuleManager moduleManager;
    private INetworkAddressCacheDAO networkAddressCacheDAO;

    public NetworkAddressCacheCaffeineService(ModuleManager moduleManager) {
        this.moduleManager = moduleManager;
    }

    private INetworkAddressCacheDAO getNetworkAddressCacheDAO() {
53
        if (isNull(networkAddressCacheDAO)) {
彭勇升 pengys 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            this.networkAddressCacheDAO = moduleManager.find(StorageModule.NAME).getService(INetworkAddressCacheDAO.class);
        }
        return this.networkAddressCacheDAO;
    }

    public int getAddressId(String networkAddress) {
        int addressId = 0;
        try {
            Integer value = addressCache.get(networkAddress, key -> getNetworkAddressCacheDAO().getAddressId(key));
            addressId = value == null ? 0 : value;

            if (addressId == 0) {
                addressId = getNetworkAddressCacheDAO().getAddressId(networkAddress);
                if (addressId != 0) {
                    addressCache.put(networkAddress, addressId);
                }
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }

        return addressId;
    }

78
    private final Cache<Integer, NetworkAddress> idCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(1000).maximumSize(5000).build();
彭勇升 pengys 已提交
79

80 81
    public NetworkAddress getAddress(int addressId) {
        NetworkAddress networkAddress = null;
彭勇升 pengys 已提交
82 83 84 85 86 87
        try {
            networkAddress = idCache.get(addressId, key -> getNetworkAddressCacheDAO().getAddressById(key));
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }

88
        if (isNull(networkAddress)) {
彭勇升 pengys 已提交
89 90 91 92 93 94 95 96
            networkAddress = getNetworkAddressCacheDAO().getAddressById(addressId);
            if (StringUtils.isNotEmpty(networkAddress)) {
                idCache.put(addressId, networkAddress);
            }
        }
        return networkAddress;
    }
}