Ignite缓存管理初体验:ignite服务端配置,大家可以用参考官方进行配置(或者使用默认配置也可以)。

本文中的ignite使用版本是1.7,与spring结合使用。
maven依赖配置

ignite 客户端配置

  1. <property name="clientMode" value="true"><!--声明为客户端-->
  2.  
  3. <!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
  4. <property name="discoverySpi">
  5. <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
  6. <property name="ipFinder">
  7. <!--
  8. Ignite provides several options for automatic discovery that can be used
  9. instead os static IP based discovery. For information on all options refer
  10. to our documentation: https://apacheignite.readme.io/docs/cluster-config
  11. -->
  12. <!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
  13. <!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
  14. <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
  15. <property name="addresses">
  16. <list>
  17. <!-- In distributed environment, replace with actual host IP address. -->
  18. <value>${ignite.serverIp}</value><!--服务端IP地址-->
  19. </list>
  20. </property>
  21. </bean>
  22. </property>
  23. </bean>
  24. </property>
  25. <property name="communicationSpi">
  26. <bean class="org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi">
  27. <property name="localPort" value="${ignite.communication.localport}"><!--对接服务端端口-->
  28. <property name="localAddress" value="192.168.1.93"><!--ignite客户端链接地址(适用多网卡情况)-->
  29. </property></property></bean>
  30. </property>
  31.  
  32. </property>

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

  1. @原码
  2. public interface IgniteCacheApplication {
  3.  
  4. /**
  5. *设置系统对象
  6. */
  7. public boolean putIgniteCache(String parentName,String name,Object value,Long expiry);
  8. /**
  9. *获取系统对象
  10. */
  11. public Object getIgniteCache(String parentName,String name);
  12.  
  13. /**
  14. *移除系统对象
  15. */
  16. public boolean removeIgniteCache(String parentName,String name);
  17. /**
  18. *清空系统对象
  19. */
  20. public boolean clearIgniteCacheByName(String parentName,String name);
  21. /**
  22. *清空系统缓存
  23. */
  24. public void clearIgniteCache();
  25. /**
  26. *销毁缓存对象
  27. */
  28. public void destroyCache(String parentName);

@实现类原码

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

  1. private static final Logger logger = Logger.getLogger(IgniteCacheServiceImpl.class);
  2.  
  3. public static Ignite ignite ;
  4.  
  5. @Autowired
  6. private IgniteConfiguration cfg;
  7.  
  8. @SuppressWarnings({ "unchecked", "rawtypes" })
  9. @Override
  10. public boolean putIgniteCache(String parentName, String name, Object value, Long expiry) {
  11. boolean flag=true;
  12. try {
  13. CacheConfiguration cacheCfg = new CacheConfiguration();
  14. if(StringUtils.isEmpty(name)){
  15. return false;
  16. }
  17. if(!StringUtils.isEmpty(expiry)){
  18. cacheCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, expiry)));
  19. }
  20. cacheCfg.setName(parentName);
  21. cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  22. IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg);
  23. cache.put(name, value);
  24. CacheConstant.igniteStatus="normal";
  25. } catch (CacheException e) {
  26. if (e.getCause() instanceof IgniteClientDisconnectedException) {
  27. IgniteClientDisconnectedException cause =
  28. (IgniteClientDisconnectedException)e.getCause();
  29. cause.reconnectFuture().get(); // Wait for reconnect.
  30. logger.error("ignite Wait for reconnect",e);
  31. CacheConstant.igniteStatus="death";
  32. flag=false;
  33. }else{
  34. e.printStackTrace();
  35. logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
  36. CacheConstant.igniteStatus="death";
  37. flag=false;
  38. }
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. logger.error("添加ignite对象缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
  42. CacheConstant.igniteStatus="death";
  43. flag=false;
  44. }
  45. return flag;
  46. }
  47. @SuppressWarnings({ "unchecked", "rawtypes" })
  48. @Override
  49. public Object getIgniteCache(String parentName, String name) {
  50. try {
  51. CacheConfiguration cacheCfg = new CacheConfiguration();
  52. if(StringUtils.isEmpty(name)){
  53. return false;
  54. }
  55. cacheCfg.setName(parentName);
  56. cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  57. IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg);
  58. CacheConstant.igniteStatus="normal";
  59. return cache.get(name);
  60. } catch (CacheException e) {
  61. if (e.getCause() instanceof IgniteClientDisconnectedException) {
  62. IgniteClientDisconnectedException cause =
  63. (IgniteClientDisconnectedException)e.getCause();
  64. cause.reconnectFuture().get(); // Wait for reconnect.
  65. logger.error("ignite Wait for reconnect",e);
  66. CacheConstant.igniteStatus="death";
  67. return null;
  68. }else{
  69. e.printStackTrace();
  70. logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
  71. CacheConstant.igniteStatus="death";
  72. return null;
  73. }
  74. } catch (Exception e) {
  75. e.printStackTrace();
  76. logger.error("获取ignite对象缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
  77. CacheConstant.igniteStatus="death";
  78. return null;
  79. }
  80. }
  81. @SuppressWarnings({ "unchecked", "rawtypes" })
  82. @Override
  83. public boolean removeIgniteCache(String parentName, String name) {
  84. try {
  85. CacheConfiguration cacheCfg = new CacheConfiguration();
  86. if(StringUtils.isEmpty(name)){
  87. return false;
  88. }
  89. cacheCfg.setName(parentName);
  90. cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  91. IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg);
  92. boolean flag = cache.remove(name);
  93. CacheConstant.igniteStatus="normal";
  94. return flag;
  95. } catch (CacheException e) {
  96. if (e.getCause() instanceof IgniteClientDisconnectedException) {
  97. IgniteClientDisconnectedException cause =
  98. (IgniteClientDisconnectedException)e.getCause();
  99. cause.reconnectFuture().get(); // Wait for reconnect.
  100. logger.error("ignite Wait for reconnect",e);
  101. return false;
  102. }else{
  103. e.printStackTrace();
  104. logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
  105. CacheConstant.igniteStatus="death";
  106. return false;
  107. }
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. logger.error("移除ignite对象缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
  111. CacheConstant.igniteStatus="death";
  112. return false;
  113. }
  114. }
  115.  
  116. @SuppressWarnings({ "unchecked", "rawtypes" })
  117. @Override
  118. public boolean clearIgniteCacheByName(String parentName, String name) {
  119. boolean flag=true;
  120. try {
  121. CacheConfiguration cacheCfg = new CacheConfiguration();
  122. cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  123. cacheCfg.setName(parentName);
  124. IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg);
  125. if(StringUtils.isEmpty(name)){
  126. return false;
  127. }
  128. cache.clear();
  129. CacheConstant.igniteStatus="normal";
  130. } catch (CacheException e) {
  131. if (e.getCause() instanceof IgniteClientDisconnectedException) {
  132. IgniteClientDisconnectedException cause =
  133. (IgniteClientDisconnectedException)e.getCause();
  134. cause.reconnectFuture().get(); // Wait for reconnect.
  135. logger.error("ignite Wait for reconnect",e);
  136. CacheConstant.igniteStatus="death";
  137. return false;
  138. }else{
  139. e.printStackTrace();
  140. logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
  141. CacheConstant.igniteStatus="death";
  142. return false;
  143. }
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. logger.error("清除ignite对象中所有缓存异常 param{"+parentName+","+name+"},errorInfo:"+e.getMessage(),e);
  147. CacheConstant.igniteStatus="death";
  148. flag=false;
  149. }
  150. return flag;
  151. }
  152.  
  153. @Override
  154. public void clearIgniteCache() {
  155. try {
  156. @SuppressWarnings("rawtypes")
  157. CacheConfiguration cacheCfg = new CacheConfiguration();
  158. cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
  159. @SuppressWarnings("unchecked")
  160. IgniteCache<string, object=""> cache = ignite.getOrCreateCache(cacheCfg);
  161. cache.clear();
  162. CacheConstant.igniteStatus="normal";
  163. } catch (CacheException e) {
  164. if (e.getCause() instanceof IgniteClientDisconnectedException) {
  165. IgniteClientDisconnectedException cause =
  166. (IgniteClientDisconnectedException)e.getCause();
  167. cause.reconnectFuture().get(); // Wait for reconnect.
  168. logger.error("ignite Wait for reconnect",e);
  169. CacheConstant.igniteStatus="death";
  170. }else{
  171. e.printStackTrace();
  172. logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
  173. CacheConstant.igniteStatus="death";
  174. }
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. logger.error("清除ignite缓存异常,errorInfo:"+e.getMessage(),e);
  178. CacheConstant.igniteStatus="death";
  179. }
  180. }
  181. /**
  182. *系统启动时,扫描ignite配置,并启动
  183. */
  184. @Override
  185. public void afterPropertiesSet() throws Exception {
  186. startIgnite();
  187. }
  188. public void setCfg(IgniteConfiguration cfg) {
  189. this.cfg = cfg;
  190. }
  191.  
  192. public void startIgnite(){
  193. logger.info("starting ignite ...");
  194. ignite = Ignition.start(cfg);
  195. logger.info("started ignite ...");
  196. }
  197. @Override
  198. public void destroyCache(String parentName) {
  199. try {
  200. ignite.destroyCache(parentName);
  201. CacheConstant.igniteStatus="normal";
  202. } catch (CacheException e) {
  203. if (e.getCause() instanceof IgniteClientDisconnectedException) {
  204. IgniteClientDisconnectedException cause =
  205. (IgniteClientDisconnectedException)e.getCause();
  206. cause.reconnectFuture().get(); // Wait for reconnect.
  207. logger.error("ignite Wait for reconnect",e);
  208. CacheConstant.igniteStatus="death";
  209. }else{
  210. e.printStackTrace();
  211. logger.error("ignite cache Exception errorInfo:"+e.getMessage(),e);
  212. CacheConstant.igniteStatus="death";
  213. }
  214. } catch (Exception e) {
  215. e.printStackTrace();
  216. logger.error("清除ignite缓存异常,errorInfo:"+e.getMessage(),e);
  217. CacheConstant.igniteStatus="death";
  218. }
  219. }
  220. </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. Scala中集合类型与java中集合类型转换

    对于java中的集合元素并不能在scala中拿来就用的,需要进行相应的转换. 1. 转换规则如下 从下面可以看出,有些可以相互转换的,有些只能单向转换: scala.collection.Iterab ...

  2. 在linux 列出 超级用户 普通用户和 系统用户

    #!/bin/bash echo Please select which list you want to print echo "1. admin (enter 1)" echo ...

  3. java 包 和 物理目录 解惑

    今天做 JUnit 实验, 发现在物理实际不同的目录(src, testsrc)下可以使用相同的包名, 并且在这两个目录下, 都有个子目录 coolUnit (这个子目录是配合 package 使用的 ...

  4. jquery mobile小经验

    现在网站上关于jquery mobile的demo和帖子可真少啊,我刚开始接触,遇到了一些问题,都找不到人请教. 这是我的个人经验总结,或多或少会对刚入门的童鞋有点帮助吧. 如果想一开始进入页面的时候 ...

  5. 从此sudo不再输密码

    #sudo visudo 最后一次输入密码. 在最后一行加入: xxx ALL=NOPASSWD: ALL xxx即为你当前使用的用户名,Ctrl+X,保存退出. 从此告别每次都要输密码的时代.

  6. NANDflash和NORflash的区别(设计师在使用闪存时需要慎重选择)

    NANDflash和NORflash的区别(设计师在使用闪存时需要慎重选择)     NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底 ...

  7. jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法

    本文实例讲述了jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法.分享给大家供大家参考.具体实现方法如下: 这里主要通过val方法设置按钮的文字,并用attr方法修改disabled属性实 ...

  8. 使用cordova+Ionic+AngularJs进行Hybird App开发的环境搭建手冊

    一.所需工具 1,JDK:生成 2.安卓SDK开发环境 3,NodeJs:主要使用的还是npm 4,Python开发环境 5.VS 2012(2008,2015也能够,已亲測):安装这个主要是须要一些 ...

  9. SmartGit Mac、Liunx、Windows过期后破解方法

    根据自己的操作系统,进入相应的文件夹 ,可能还有一个版本号的文件夹,再进入 Windows: %APPDATA%\syntevo\SmartGit\ OS X: ~/Library/Preferenc ...

  10. vue-router scrollBehavior无效的问题

    在使用vue做单页面应用开发时候 使用vue-router作为路由控制器  在使用过程中发现每个页面打开都在原来的位置 不能返回到页面顶部位置 ,然后查看api文档 滚动行为  发现如下代码: con ...