赞
踩
Infinispan 其实就是一个分布式缓存和数据网格平台,提供了高度可扩展和高性能数据缓存解决方案。Infinispan可以作为本地缓存或分布式缓存使用,支持事务、查询、处理大数据等功能。简单地说,Infinispan 可以理解为是 MySQL 的内存版本。
Infinispan 与 Redis 都是一种缓存解决方案或工具,但两者之间存在着许多不同的地方。
在 Infinispan 中,有一个缓存管理器叫做 CacheManager,能够用于创建和控制使用不同的缓存模式,比如本地缓存、分布式缓存、失效缓存等。
项目demo地址:https://gitee.com/regexpei/infinispan-demo
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-spring-boot-starter-embedded</artifactId>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-query</artifactId>
</dependency>
@Configuration public class InfinispanCacheConfig { @Bean public InfinispanGlobalConfigurationCustomizer globalCustomizer() { return builder -> builder.clusteredDefault().transport() .clusterName("MyCluster") .machineId("myMachine") .rackId("Rack").siteId("China").build(); } @Bean public InfinispanCacheConfigurer cacheConfigurer() { return manager -> { final org.infinispan.configuration.cache.Configuration ispnConfig = new ConfigurationBuilder() .clustering() .cacheMode(CacheMode.LOCAL) // 设置缓存模式 .persistence() // 开启持久化 .passivation(false) // 缓存数据时持久化数据 .addSoftIndexFileStore() // 使用软索引文件存储方式 .dataLocation("cache-data") // 持久化数据存储位置 .indexLocation("cache-index") // 持久化索引存储位置 .shared(false) // 不共享持久化存储 .memory().maxCount(1000000L) // 设置缓存最大数量, 1000000L 表示存储一百万条数据 .build(); manager.defineConfiguration("local-sync-config", ispnConfig); }; } @Bean(name = "large-cache") public org.infinispan.configuration.cache.Configuration largeCache() { return new ConfigurationBuilder() .clustering().cacheMode(CacheMode.LOCAL) .indexing().enable().storage(IndexStorage.FILESYSTEM).path("my-fs-cache-inf") .addIndexedEntities(UserInfoEntity.class) .memory().maxCount(1000000L) .build(); } }
@Data @Table(value = "user_info") @Indexed public class UserInfoEntity implements Serializable { @Id(keyType = KeyType.Auto) private Long id; @KeywordField private String name; private String idCard; private String phoneNumber; @FullTextField private String desc; private String email; private String bankCard; @GenericField private LocalDateTime updateTime; private LocalDateTime createTime; }
@Indexed
注解@KeywordField
:关键词索引,建议用于内容只有数字和字母的字段上,比如id、taskNo,数据类型只能是String@FullTextField
:全文索引,建议用在需要模糊匹配、文本检索的字段上,比如taskName,对于纯数字字母内容的字段,只能全匹配,模糊匹配会失效,数据类型只能是String@GenericField
:普通索引,建议除了添加@KeywordField和@FullTextField注解之外的字段都加上这个索引,因为SQL中出现使用@FullTextField注解的字段不能和没有添加注解的字段起使用@Slf4j @RestController @RequestMapping("users") public class UserController { @Autowired UserInfoMapper userInfoMapper; @Autowired UserInfoService userInfoService; @Autowired EmbeddedCacheManager cacheManager; @GetMapping("{id}") public UserInfoEntity getOne(@PathVariable Long id){ return userInfoMapper.selectOneByCondition(USER_INFO_ENTITY.ID.eq(id)); } @GetMapping("add-cache") public String addCache(@RequestParam long page, @RequestParam(defaultValue = "10000") long pageSize){ long count = userInfoMapper.selectCountByCondition(QueryCondition.createEmpty()); long total = count / pageSize + 1; log.info("分页总数:{}", total); int coreSize = Runtime.getRuntime().availableProcessors() * 2 + 1; ThreadPoolExecutor executor = ThreadUtil.newExecutor(coreSize, coreSize + 5); for (long i = 1; i <= total; i++) { long finalI = i; executor.execute(() -> userInfoService.addToCache(finalI, pageSize)); } log.info("缓存完成!"); return "ok"; } @PostMapping("search") public List<UserInfoEntity> query(@RequestBody Map<String, Object> parameters){ String sql = (String) parameters.get("sql"); Integer page = (Integer) parameters.get("page"); Integer pageSize = (Integer) parameters.get("pageSize"); return userInfoService.search(sql, page, pageSize); } @DeleteMapping("{id}") public int del(@PathVariable Long id){ return userInfoMapper.deleteByCondition(USER_INFO_ENTITY.ID.eq(id)); } @GetMapping("count") public long count() { Cache<String, UserInfoEntity> cache = cacheManager.getCache("large-cache"); OptionalLong optionalLong = Search.getQueryFactory(cache) .create("from cn.regexp.infinispan.entity.UserInfoEntity") .execute().hitCount(); return optionalLong.orElse(0L); } @GetMapping("clear") public String clear() { Cache<String, UserInfoEntity> cache = cacheManager.getCache("large-cache"); cache.clear(); return "ok"; } }
ISPN000343: Must have a transport set in the global configuration in order to define a clustered cache
infinispan.xml配置文件添加以下内容:
<infinispan xmlns="urn:infinispan:config:13.0">
<cache-container>
<transport cluster="MyCluster"
machine="LinuxServer01"
rack="Rack01"
site="US-WestCoast"/>
</cache-container>
</infinispan>
ISPN000403: No indexable classes were defined for this indexed cache. The configuration must contain classes or protobuf message types annotated with ‘@Indexed’
infinispan.xml配置文件添加以下内容:
<infinispan xmlns="urn:infinispan:config:13.0"> <cache-container> <replicated-cache segments="256" mode="SYNC" statistics="true" name="replicatedCache"> <indexing enabled="true" storage="local-heap"> <index-reader refresh-interval="1000"/> <indexed-entities> <indexed-entity>cn.regexp.infinispan.entity.UserInfoEntity</indexed-entity> </indexed-entities> </indexing> </replicated-cache> </cache-container> </infinispan>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。