Ignite缓存管理 基础缓存使用教程

2 篇文章 0 订阅
1 篇文章 1 订阅

api中文翻译官网:https://www.zybuluo.com/liyuj/note/230739
官方网站:http://ignite.apache.org/

ignite服务端配置,大家可以用参考官方进行配置(或者使用默认配置也可以)。
本文中的ignite使用版本是1.7,与spring结合使用。
maven依赖配置

这里写图片描述

ignite 客户端配置

    <property name="clientMode" value="true"/><!--声明为客户端-->

    <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
    <property name="discoverySpi">
        <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
            <property name="ipFinder">
                <!--
                    Ignite provides several options for automatic discovery that can be used
                    instead os static IP based discovery. For information on all options refer
                    to our documentation: http://apacheignite.readme.io/docs/cluster-config
                -->
                <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
                <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
                <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
                    <property name="addresses">
                        <list>
                            <!-- In distributed environment, replace with actual host IP address. -->
                            <value>${ignite.serverIp}</value><!--服务端IP地址-->
                        </list>
                    </property>
                </bean>
            </property>
        </bean>
    </property>
    <property name="communicationSpi">
        <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi">
            <property name="localPort" value="${ignite.communication.localport}" /><!--对接服务端端口-->
            <property name="localAddress" value="192.168.1.93" /><!--ignite客户端链接地址(适用多网卡情况)-->
        </bean>
    </property>
</bean>

igniteService代码(interface实现缓存存储,供应用调用)

@原码
public interface IgniteCacheApplication {

/**
*设置系统对象
*/
public boolean putIgniteCache(String parentName,String name,Object value,Long expiry);
/**
*获取系统对象
*/
public Object getIgniteCache(String parentName,String name);

/**
*移除系统对象
*/
public boolean removeIgniteCache(String parentName,String name);

/**
*清空系统对象
*/
public boolean clearIgniteCacheByName(String parentName,String name);
/**
*清空系统缓存
*/
public void clearIgniteCache();
/**
*销毁缓存对象
*/
public void destroyCache(String parentName);
}

@实现类原码

@Service(“cacheServiceImpl”)
public class IgniteCacheServiceImpl implements CacheApplication,InitializingBean {

private static final Logger logger = Logger.getLogger(IgniteCacheServiceImpl.class);

public static Ignite ignite ;

@Autowired
private IgniteConfiguration cfg;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public boolean putIgniteCache(String parentName, String name, Object value, Long expiry) {
    boolean flag=true;
    try {
        CacheConfiguration cacheCfg = new CacheConfiguration();
        if(StringUtils.isEmpty(name)){
            return false;
        }
        if(!StringUtils.isEmpty(expiry)){
            cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, expiry)));
        }
        cacheCfg.setName(parentName);
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        IgniteCache<String, Object> cache = ignite.getOrCreateCache(cacheCfg);
        cache.put(name, value);
        CacheConstant.igniteStatus="normal";
    } catch (CacheException e) {
        if (e.getCause() instanceof IgniteClientDisconnectedException) {
            IgniteClientDisconnectedException cause =
              (IgniteClientDisconnectedException)e.getCause();
            cause.reconnectFuture().get(); // Wait for reconnect.
            logger.error("ignite Wait for reconnect",e);
            CacheConstant.igniteStatus="death";
            flag=false;
          }else{
              e.printStackTrace();
              logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
              CacheConstant.igniteStatus="death";
              flag=false;
          }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("添加ignite对象缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
        CacheConstant.igniteStatus="death";
        flag=false;
    }
    return flag;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Object getIgniteCache(String parentName, String name) {
    try {
        CacheConfiguration cacheCfg = new CacheConfiguration();
        if(StringUtils.isEmpty(name)){
            return false;
        }
        cacheCfg.setName(parentName);
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        IgniteCache<String, Object> cache = ignite.getOrCreateCache(cacheCfg);
        CacheConstant.igniteStatus="normal";
        return cache.get(name);
    } catch (CacheException e) {
        if (e.getCause() instanceof IgniteClientDisconnectedException) {
            IgniteClientDisconnectedException cause =
              (IgniteClientDisconnectedException)e.getCause();
            cause.reconnectFuture().get(); // Wait for reconnect.
            logger.error("ignite Wait for reconnect",e);
            CacheConstant.igniteStatus="death";
            return null;
          }else{
              e.printStackTrace();
              logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
              CacheConstant.igniteStatus="death";
              return null;
          }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("获取ignite对象缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
        CacheConstant.igniteStatus="death";
        return null;
    }
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public boolean removeIgniteCache(String parentName, String name) {
    try {
        CacheConfiguration cacheCfg = new CacheConfiguration();
        if(StringUtils.isEmpty(name)){
            return false;
        }
        cacheCfg.setName(parentName);
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        IgniteCache<String, Object> cache = ignite.getOrCreateCache(cacheCfg);
        boolean flag = cache.remove(name);
        CacheConstant.igniteStatus="normal";
        return flag;
    } catch (CacheException e) {
        if (e.getCause() instanceof IgniteClientDisconnectedException) {
            IgniteClientDisconnectedException cause =
              (IgniteClientDisconnectedException)e.getCause();
            cause.reconnectFuture().get(); // Wait for reconnect.
            logger.error("ignite Wait for reconnect",e);
            return false;
          }else{
              e.printStackTrace();
              logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
              CacheConstant.igniteStatus="death";
              return false;
          }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("移除ignite对象缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
        CacheConstant.igniteStatus="death";
        return false;
    }
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public boolean clearIgniteCacheByName(String parentName, String name) {
    boolean flag=true;
    try {
        CacheConfiguration cacheCfg = new CacheConfiguration();
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        cacheCfg.setName(parentName);
        IgniteCache<String, Object> cache = ignite.getOrCreateCache(cacheCfg);
        if(StringUtils.isEmpty(name)){
            return false;
        }
        cache.clear();
        CacheConstant.igniteStatus="normal";
    } catch (CacheException e) {
        if (e.getCause() instanceof IgniteClientDisconnectedException) {
            IgniteClientDisconnectedException cause =
              (IgniteClientDisconnectedException)e.getCause();
            cause.reconnectFuture().get(); // Wait for reconnect.
            logger.error("ignite Wait for reconnect",e);
            CacheConstant.igniteStatus="death";
            return false;
          }else{
              e.printStackTrace();
              logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
              CacheConstant.igniteStatus="death";
              return false;
          }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("清除ignite对象中所有缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
        CacheConstant.igniteStatus="death";
        flag=false;
    }
    return flag;
}

@Override
public void clearIgniteCache() {
    try {
        @SuppressWarnings("rawtypes")
        CacheConfiguration cacheCfg = new CacheConfiguration();
        cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
        @SuppressWarnings("unchecked")
        IgniteCache<String, Object> cache = ignite.getOrCreateCache(cacheCfg);
        cache.clear();
        CacheConstant.igniteStatus="normal";
    } catch (CacheException e) {
        if (e.getCause() instanceof IgniteClientDisconnectedException) {
            IgniteClientDisconnectedException cause =
              (IgniteClientDisconnectedException)e.getCause();
            cause.reconnectFuture().get(); // Wait for reconnect.
            logger.error("ignite Wait for reconnect",e);
            CacheConstant.igniteStatus="death";
          }else{
              e.printStackTrace();
              logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
              CacheConstant.igniteStatus="death";
          }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("清除ignite缓存异常,errorInfo:"+e.getMessage(),e);
        CacheConstant.igniteStatus="death";
    }
}
/**
*系统启动时,扫描ignite配置,并启动
*/
@Override
public void afterPropertiesSet() throws Exception {
    startIgnite();
}
public void setCfg(IgniteConfiguration cfg) {
    this.cfg = cfg;
}

public void startIgnite(){
    logger.info("starting ignite ...");
    ignite = Ignition.start(cfg);
    logger.info("started ignite ...");
}
@Override
public void destroyCache(String parentName) {
    try {
        ignite.destroyCache(parentName);
        CacheConstant.igniteStatus="normal";
    } catch (CacheException e) {
        if (e.getCause() instanceof IgniteClientDisconnectedException) {
            IgniteClientDisconnectedException cause =
              (IgniteClientDisconnectedException)e.getCause();
            cause.reconnectFuture().get(); // Wait for reconnect.
            logger.error("ignite Wait for reconnect",e);
            CacheConstant.igniteStatus="death";
          }else{
              e.printStackTrace();
              logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
              CacheConstant.igniteStatus="death";
          }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("清除ignite缓存异常,errorInfo:"+e.getMessage(),e);
        CacheConstant.igniteStatus="death";
    }
}

}
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值