Ignite缓存持久化例子
来源:互联网 发布:php完全自学手册出版地 编辑:程序博客网 时间:2024/05/16 08:59
1、配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean class="org.apache.ignite.configuration.IgniteConfiguration" id="ignite.cfg"> <property name="cacheConfiguration"> <list> <bean class="org.apache.ignite.configuration.CacheConfiguration"> <property name="name" value="personCache"></property> <property name="readThrough" value="true"></property> <property name="writeThrough" value="true"></property> <!-- Set cacheStoreFactory--> <property name="cacheStoreFactory"> <bean class="javax.cache.configuration.FactoryBuilder" factory-method="factoryOf"> <constructor-arg value="com.peidw.ignite.store.PersonStore"></constructor-arg> </bean> </property> <property name="queryEntities"> <list> <bean class="org.apache.ignite.cache.QueryEntity"> <property name="keyType" value="java.lang.Long"></property> <property name="valueType" value="com.peidw.ignite.model.Person"></property> <property name="fields"> <map> <entry key="id" value="java.lang.Long"></entry> <entry key="orgId" value="java.lang.Integer"></entry> <entry key="name" value="java.lang.String"></entry> </map> </property> </bean> </list> </property> </bean> </list> </property> <property name="peerClassLoadingEnabled" value="true"></property> <property name="discoverySpi"> <bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi"> <property name="joinTimeout" value="200"/> <!-- 服务端不存就立刻退出--> <property name="ipFinder"> <bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder"> <property name="addresses"> <list> <value>127.0.0.1:47500</value> </list> </property> </bean> </property> </bean> </property> </bean></beans>
实现的代码:
1、模型,Person.java,从上面的配置中我们可以看得出是使用了mysql数据库
DROP TABLE IF EXISTS `person`;CREATE TABLE `person` ( `id` bigint(20) NOT NULL, `orgId` bigint(20) DEFAULT NULL, `name` varchar(200) CHARACTER SET latin1 DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
package com.peidw.ignite.model;/** * Created by ThinkPad on 2017-08-28. */public class Person { private long id; private int orgId; private String name; public Person(){ } public Person(long vid,int vorgId,String vname){ this.id=vid; this.orgId=vorgId; this.name=vname; } public long getId() { return id; } public void setId(long id) { this.id = id; } public int getOrgId() { return orgId; } public void setOrgId(int orgId) { this.orgId = orgId; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
PersonStore.java缓存实现类
package com.peidw.ignite.store;import com.peidw.ignite.model.Person;import org.apache.ignite.cache.store.CacheStore;import org.apache.ignite.lang.IgniteBiInClosure;import org.apache.ignite.resources.SpringResource;import org.jetbrains.annotations.Nullable;import javax.cache.Cache;import javax.cache.integration.CacheLoaderException;import javax.cache.integration.CacheWriterException;import javax.sql.DataSource;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Collection;import java.util.Map;/** * 缓存工厂 * Created by peidw on 2017-08-28. */public class PersonStore implements CacheStore<Long, Person> { @SpringResource(resourceName = "dataSource") private DataSource dataSource; @Override public void loadCache(IgniteBiInClosure<Long, Person> igniteBiInClosure, @Nullable Object... objects) throws CacheLoaderException { System.out.println(">> Loading cache from store..."); try { Connection conn = dataSource.getConnection(); PreparedStatement st = conn.prepareStatement("select * from PERSON"); ResultSet rs = st.executeQuery(); while (rs.next()) { System.out.println("----------------->"+rs.getString(3)); Person person = new Person(rs.getLong(1), rs.getInt(2), rs.getString(3)); igniteBiInClosure.apply(person.getId(), person); } } catch (Exception e) { throw new CacheLoaderException("Failed to load values from cache store.", e); } } @Override public void sessionEnd(boolean b) throws CacheWriterException { } @Override public Person load(Long aLong) throws CacheLoaderException { System.out.println(">> Loading person from store..."); try (Connection conn = dataSource.getConnection()) { try (PreparedStatement st = conn.prepareStatement("select * from PERSON where id = ?")) { st.setString(1, aLong.toString()); ResultSet rs = st.executeQuery(); return rs.next() ? new Person(rs.getLong(1), rs.getInt(2), rs.getString(3) ) : null; } } catch (SQLException e) { throw new CacheLoaderException("Failed to load values from cache store.", e); } } @Override public Map<Long, Person> loadAll(Iterable<? extends Long> iterable) throws CacheLoaderException { return null; } @Override public void write(Cache.Entry<? extends Long, ? extends Person> entry) throws CacheWriterException { System.out.println(">> Loading person from store..."); try (Connection conn = dataSource.getConnection()) { PreparedStatement st = conn.prepareStatement("insert into PERSON(id,orgId,name)VALUE (?,?,?) "); st.setLong((int)1,(long)entry.getKey()); st.setInt((int)2,entry.getValue().getOrgId()); st.setString((int)3,entry.getValue().getName()); st.executeUpdate(); System.out.println(">> add person to store..."); } catch (SQLException e) { throw new CacheLoaderException("Failed to add person to table store.", e); } } @Override public void writeAll(Collection<Cache.Entry<? extends Long, ? extends Person>> collection) throws CacheWriterException { } @Override public void delete(Object o) throws CacheWriterException { } @Override public void deleteAll(Collection<?> collection) throws CacheWriterException { }}
package com.peidw.ignite;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteException;import org.apache.ignite.Ignition;/** * Created by ThinkPad on 2017-08-28. */public class PersonStoreServer { public static void main(String[] args) throws IgniteException { Ignite ignite = Ignition.start("E:/Program Files (x86)/JetBrains/mywk/stud/igniteDemo/src/main/resources/Read-Through.xml"); }}
PersonStoreClient.java
package com.peidw.ignite;import com.peidw.ignite.model.Person;import org.apache.ignite.Ignite;import org.apache.ignite.IgniteCache;import org.apache.ignite.IgniteException;import org.apache.ignite.Ignition;import org.apache.ignite.cache.query.QueryCursor;import org.apache.ignite.cache.query.SqlFieldsQuery;import java.util.List;/** * * Created by peidw on 2017-08-28. */public class PersonStoreClient { public static void main(String[] args) throws IgniteException { Ignition.setClientMode(true); Ignite ignite = Ignition.start("E:\\Program Files (x86)\\JetBrains\\mywk\\stud\\igniteDemo\\src\\main\\resources\\Read-Through.xml"); IgniteCache<Long, Person> cache = ignite.getOrCreateCache("personCache"); cache.loadCache(null); QueryCursor<List<?>> cursor = cache.query(new SqlFieldsQuery("select id, orgId,name from Person")); System.out.println(cursor.getAll()); Person p=new Person(44,5,"CCED"); cache.put(p.getId(),p); ignite.close(); }}
即可运行测试
官方文档,参考:https://www.zybuluo.com/liyuj/note/785629
阅读全文
0 0
- Ignite缓存持久化例子
- Ignite 缓存最简单的例子
- Apache Ignite:如何从持久化存储中读取数据
- android持久化缓存
- osworkflow持久化例子
- Ignite缓存管理 基础缓存使用教程
- Ignite
- spark持久化(缓存)
- redis缓存数据持久化
- 分布式缓存ignite与其他缓存特效比较
- Flex3 新功能—framework持久化缓存
- hibernate 缓存——数据持久化
- Hibernate缓存,持久化引发的问题
- Spark Streaming:缓存与持久化机制
- 用 webpack 实现持久化缓存
- 用 webpack 实现持久化缓存
- 分布式缓存Redis之持久化
- Apache Ignite——新一代数据库缓存系统
- ServletContextListener接口
- python中double转化成float32
- RabbitMQ的几种典型使用场景
- CSS实现单行、多行文本溢出时显示省略号
- 洛谷P2723 丑数 Humble Numbers
- Ignite缓存持久化例子
- <数据结构与算法>单向循环链表基本框架(C语言描述)
- 【帝国CMS】排行榜调用标签(24小时排行、本周排行、本月排行)
- 动态规划算法
- SIGIR 2017 Paper Characterizing and Predicting Enterprise Email Reply Behavior
- git
- 2017年8月28日22:46:43
- Expect ssh免秘钥登入自动脚本(ssh-copy-id、expect免交互输入脚本)
- 01_SpringMVC流程架构图