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,>

Ignite缓存管理初体验的更多相关文章

  1. 图数据库HugeGraph:HugeGraph-Hubble基于Web的可视化图管理初体验

    原创/朱季谦 一.HugeGraph-Hubble简介 关于HugeGraph,官方资料是这样介绍的,它是一款易用.高效.通用的开源图数据库系统(Graph Database), 实现了 Apache ...

  2. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

  3. Ignite缓存大小管理

    Ignite使用计算机内存存储缓存数据,达到提升缓存读写性能的.但是计算机内存往往是有限的,我们必须合理管理Ignite对内存的使用. Ignite可以使用JVM堆外内存和堆内内存.使用堆外内存基本上 ...

  4. Helm Template初体验,方便管理多环境

    我最新最全的文章都在南瓜慢说 www.pkslow.com,文章更新也只在官网,欢迎大家来喝茶~~ 1 简介 Helm作为一个优秀的包管理器,这部分我们之前已经做了介绍,文章如下: 用Helm部署Ku ...

  5. 五、MyBatis缓存初体验

    缓存就是内存中的数据,常常来自对数据库查询结果的保存,使用缓存, 我们可以避免频繁的与数据库进行交互, 进而提高响应速度. 一级缓存初体验(session,默认打开) 同一查询执行两次以上:selec ...

  6. Bower管理依赖库初体验

    比如一开始我用了jquery-1.10.2.min.js,后来要用bootstrap,但bootstrap依赖的确实2.0.3版本的jquery,那又要下载一个去替换原来的,这样的事情发生多了就会觉得 ...

  7. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  8. node.js 初体验

    node.js 初体验 2011-10-31 22:56 by 聂微东, 174545 阅读, 118 评论, 收藏, 编辑 PS: ~ 此篇文章的进阶内容在为<Nodejs初阶之express ...

  9. grunt 构建工具(build tool)初体验

    操作环境:win8 系统,建议使用 git bash (window下的命令行工具) 1,安装node.js 官网下载:https://nodejs.org/  直接点击install ,会根据你的操 ...

随机推荐

  1. PHP——修改数据库2-加提示框,加登录页面

    登录页面:0127lianxi.php <body> <h1>登陆</h1> <form action="0127lianxi.php" ...

  2. asp.net 正在加载效果实现

    最近研究了下asp.net 正在加载的实现原理,总结了以下实现方法 首先,我们有个div显示内容为正在加载..   当然也可以考虑用图片或者其他的,不过考虑到速度,建议直接文字提示就行,然后设置div ...

  3. 你所不知道的JSON

    译者按: 老司机们,你知道JSON.stringify还有第二个和第三个可选参数吗?它们是什么呢? JSON已经逐渐替代XML被全世界的开发者广泛使用.本文深入讲解JavaScript中使用JSON. ...

  4. 数据库 proc编程六

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  5. Softmax回归推导过程

    http://www.cnblogs.com/Deep-Learning/p/7073744.html http://www.cnblogs.com/lutingting/p/4768882.html ...

  6. Servlet和JSP规范及版本对应关系

    JSR 53: JavaTM Servlet 2.3 and JavaServer PagesTM 1.2 JSR 154: JavaTM Servlet 2.4 JSR 154: JavaTM Se ...

  7. C#中动态调用DLL动态链接库

    其中要使用两个未公开的Win32 API函数来存取控制台窗口,这就需要使用动态调用的方法,动态调用中使用的Windows API函数主要有三个,即:Loadlibrary,GetProcAddress ...

  8. XAML 中使用空格键(空白字符)

    默认情况下,XAML折叠所有空白,这意味着包含空格.tab 键以及回车的长字符串将被转换为单个空格.而且,如果在一个元素内容之前或之后添加空白,这个空格将被完全忽略. 有时这并不是所期望的结果.例如, ...

  9. Oracle的归档日志

    归档模式的特点和要求 在归档模式下,当LGWR后台进程的写操作从一个重做日志组切换到另一个重做日志组后,归档写后台进程(ARCH/ARCRn)就会将原来的重做日志的信息复制到归档日志文件中. 可以把归 ...

  10. 面试题思考:web中关于一些容器基本概念的简单总结

    关键字:应用服务器.web服务器.web容器.jsp容器.servlet容器. 1.应用服务器: 作为应用程序服务器,要求可以通过各种协议(包括 HTTP 协议)把商业逻辑暴露给(expose)客户端 ...