zoukankan      html  css  js  c++  java
  • Ignite缓存管理初体验

    Ignite缓存管理初体验: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: https://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客户端链接地址(适用多网卡情况)-->
            </property></property></bean>
        </property>
     
    </property>

    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";
        }
    }
    </string,></string,></string,></string,></string,>
  • 相关阅读:
    HTML-图片和多媒体
    HTML弹性布局
    HTML定位——绝对定位和相对定位、固定定位
    HTML定位和布局----float浮动
    CSS层叠
    HTML-css样式引用方式
    认识HTML和CSS
    原生js 进度条
    原生js文字滚动 滚动条
    时间轴
  • 原文地址:https://www.cnblogs.com/momoyan/p/9163031.html
Copyright © 2011-2022 走看看