ehcache 自定义序列化

序列化:ehcache可在on-heap(jvm内存)、off-heap(系统内存)、disk、clustered store上存储,除在on-heap上可以直接存储对象或者对象的引用外,在其余地方存储需要对对象进行序列化

默认提供的序列化类

***********************

相关类与接口

Serializer

public interface Serializer<T> {ByteBuffer serialize(T var1) throws SerializerException;T read(ByteBuffer var1) throws ClassNotFoundException, SerializerException;boolean equals(T var1, ByteBuffer var2) throws ClassNotFoundException, SerializerException;
}

IntegerSerializer:默认的整数序列化类

public class IntegerSerializer implements Serializer<Integer> {public IntegerSerializer() {}public IntegerSerializer(ClassLoader classLoader) {}public ByteBuffer serialize(Integer object) {ByteBuffer byteBuffer = ByteBuffer.allocate(4);byteBuffer.putInt(object).flip();return byteBuffer;}public Integer read(ByteBuffer binary) {return binary.getInt();}public boolean equals(Integer object, ByteBuffer binary) {return object.equals(this.read(binary));}
}

***********************

示例

序列化类

public class PersonSerializer implements Serializer<Person> {public PersonSerializer(){}public PersonSerializer(ClassLoader classLoader){}@Overridepublic ByteBuffer serialize(Person person) throws SerializerException {return ByteBuffer.wrap(JSONObject.toJSONBytes(person));}@Overridepublic Person read(ByteBuffer byteBuffer) throws ClassNotFoundException, SerializerException {return JSONObject.parseObject(byteBuffer.array(),Person.class);}@Overridepublic boolean equals(Person person, ByteBuffer byteBuffer) throws ClassNotFoundException, SerializerException {return person.equals(JSONObject.parseObject(byteBuffer.array(),Person.class));}
}

注意:序列化类需要public修饰、且需含有classLoader构造函数

@Data
class Person{private String name;private Integer age;
}public class Test {public static void main(String[] args){CacheManager cacheManager= CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence("d:"+File.separator+"cache")).withSerializer(Person.class,PersonSerializer.class).withCache("custom", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class,Person.class,ResourcePoolsBuilder.newResourcePoolsBuilder().heap(10, MemoryUnit.MB).disk(100,MemoryUnit.MB).build())).build(true);Person person=new Person();person.setName("瓜田李下");person.setAge(20);Cache<String,Person> cache=cacheManager.getCache("custom",String.class,Person.class);cache.put("1",person);System.out.println(cache.get("1"));//cacheManager.close();}

********************

控制台输出

"C:\Program Files\Java\jdk-15\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=50959:C:\Program Files\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath "E:\java\IdeaProjects\springboot ehcache test\target\classes;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-starter-web\2.4.5\spring-boot-starter-web-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-starter\2.4.5\spring-boot-starter-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot\2.4.5\spring-boot-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-autoconfigure\2.4.5\spring-boot-autoconfigure-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-starter-logging\2.4.5\spring-boot-starter-logging-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\yaml\snakeyaml\1.27\snakeyaml-1.27.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-starter-json\2.4.5\spring-boot-starter-json-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\fasterxml\jackson\core\jackson-databind\2.11.4\jackson-databind-2.11.4.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.4\jackson-annotations-2.11.4.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\fasterxml\jackson\core\jackson-core\2.11.4\jackson-core-2.11.4.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.4\jackson-datatype-jdk8-2.11.4.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.4\jackson-datatype-jsr310-2.11.4.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.4\jackson-module-parameter-names-2.11.4.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-starter-tomcat\2.4.5\spring-boot-starter-tomcat-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.45\tomcat-embed-core-9.0.45.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.45\tomcat-embed-websocket-9.0.45.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-web\5.3.6\spring-web-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-beans\5.3.6\spring-beans-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-webmvc\5.3.6\spring-webmvc-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-aop\5.3.6\spring-aop-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-context\5.3.6\spring-context-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-expression\5.3.6\spring-expression-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\boot\spring-boot-configuration-processor\2.4.5\spring-boot-configuration-processor-2.4.5.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\projectlombok\lombok\1.18.20\lombok-1.18.20.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\ehcache\ehcache\3.9.3\ehcache-3.9.3.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\com\alibaba\fastjson\1.2.76\fastjson-1.2.76.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-core\5.3.6\spring-core-5.3.6.jar;E:\Program Files\apache-maven-3.6.1-bin\apache-maven-3.6.1\repository\org\springframework\spring-jcl\5.3.6\spring-jcl-5.3.6.jar" com.example.demo.test.Test
11:42:36.942 [main] DEBUG org.ehcache.core.spi.ServiceLocator - Starting 20 Services...
11:42:36.957 [main] DEBUG org.ehcache.impl.persistence.DefaultLocalPersistenceService - RootDirectory Locked
11:42:36.973 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Starting service
11:42:36.973 [main] DEBUG org.ehcache.core.spi.ServiceLocator - All Services successfully started, 20 Services in 31ms
11:42:36.973 [main] DEBUG org.ehcache.core.EhcacheManager - Creating Cache 'custom' in EhcacheManager.
11:42:36.989 [main] DEBUG org.ehcache.impl.persistence.DefaultLocalPersistenceService - Destroying file based persistence context for custom
11:42:36.989 [main] DEBUG org.ehcache.impl.persistence.FileUtils - Created d:\cache\file
11:42:36.989 [main] DEBUG org.ehcache.impl.persistence.FileUtils - Created d:\cache\file\custom_f9ac14b63a75faf57d8db6f919bfabb2502d273c
11:42:36.989 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <java.lang.String> : org.ehcache.impl.serialization.StringSerializer@5a4041cc
11:42:36.989 [main] DEBUG org.ehcache.impl.internal.spi.serialization.DefaultSerializationProvider - Serializer for <com.example.demo.test.Person> : com.example.demo.test.PersonSerializer@2f8f5f62
11:42:37.004 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <java.lang.String> : org.ehcache.impl.copy.IdentityCopier@4e4aea35
11:42:37.004 [main] DEBUG org.ehcache.impl.internal.spi.copy.DefaultCopyProvider - Copier for <com.example.demo.test.Person> : org.ehcache.impl.copy.IdentityCopier@1442d7b5
11:42:37.020 [main] INFO org.ehcache.sizeof.filters.AnnotationSizeOfFilter - Using regular expression provided through VM argument org.ehcache.sizeof.filters.AnnotationSizeOfFilter.pattern for IgnoreSizeOf annotation : ^.*cache\..*IgnoreSizeOf$
11:42:37.129 [main] INFO org.ehcache.sizeof.impl.JvmInformation - Detected JVM data model settings of: 64-Bit HotSpot JVM with Compressed OOPs
11:42:37.192 [main] INFO org.ehcache.sizeof.impl.AgentLoader - Failed to attach to VM and load the agent: class java.io.IOException: Can not attach to current VM
11:42:37.207 [main] DEBUG org.ehcache.impl.internal.store.heap.OnHeapStore - No expiration strategy detected
11:42:37.299 [main] DEBUG org.ehcache.impl.persistence.FileUtils - Created d:\cache\file\custom_f9ac14b63a75faf57d8db6f919bfabb2502d273c\offheap-disk-store
11:42:37.383 [main] DEBUG class org.ehcache.core.Ehcache-custom - Initialize successful.
11:42:37.383 [main] INFO org.ehcache.core.EhcacheManager - Cache 'custom' created in EhcacheManager.
11:42:37.383 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Moving from UNINITIALIZED to AVAILABLE
11:42:37.383 [main] DEBUG org.ehcache.core.internal.statistics.DefaultStatisticsService - Cache added custom
11:42:37.398 [main] DEBUG org.ehcache.core.EhcacheManager - Initialize successful.
11:42:37.508 [main] DEBUG org.ehcache.sizeof.filters.AnnotationSizeOfFilter - org.ehcache.sizeof.annotations.IgnoreSizeOf matched IgnoreSizeOf annotation pattern ^.*cache\..*IgnoreSizeOf$
Person(name=瓜田李下, age=20)

ehcache 自定义序列化相关推荐

  1. C#自定义序列化反序列化与 ISerializable 接口

    ISerializable 接口 MSDN注解:允许对象控制其自己的序列化和反序列化过程. ISerializable 接口的定义: public interface ISerializable {v ...

  2. java自定义外部接口_如何使用可外部化的接口在Java中自定义序列化

    java自定义外部接口 在上一篇文章"用示例介绍的有关Java序列化的一切"中 ,我解释了如何使用以下方法序列化/反序列化一个对象 Serializable接口,还说明了如何使用w ...

  3. spring序列化_使用@JsonIdentityInfo的Spring自定义序列化器

    spring序列化 介绍 Spring中从JSON到JSON的序列化/反序列化已广泛用于基于Spring的现代应用程序中. 它基于杰克逊. Jackson可以轻松地将任何POJO序列化为JSON,反之 ...

  4. 如何使用可外部化的接口在Java中自定义序列化

    在上一篇文章"用示例介绍的有关Java序列化的一切"中 ,我解释了如何使用以下方法序列化/反序列化一个对象 Serializable接口,还说明了如何使用writeObject和r ...

  5. 使用@JsonIdentityInfo的Spring自定义序列化器

    介绍 Spring中从JSON到JSON的序列化/反序列化已广泛用于基于Spring的现代应用程序中. 它基于杰克逊. Jackson可以轻松地将任何POJO序列化为JSON,反之亦然. 这段代码写得 ...

  6. java自定义序列化_Java中的自定义国际化(i18n)

    java自定义序列化 国际化(i18n)在我们的软件项目中非常重要. 它主要带来以下好处: 将UI字符串外部化为代码文件以外的外部文件,以及易于管理的UI内容. 支持多种语言. 在这篇文章中,将为Ec ...

  7. 60-40-020-序列化-自定义序列化

    1.视界 1. 案例说明 有一个 Java 实体类 Customer,定义如下: package com.bonc.rdpe.kafka110.beans;/*** @Title Customer.j ...

  8. kafka自定义序列化器

    <kafka权威指南> Customer.java public class Customer {private int customId;private String customerN ...

  9. php serializable,PHP自定义序列化接口Serializable用法分析讲解

    这篇文章主要介绍了PHP自定义序列化接口Serializable用法,结合实例形式分析了Serializable自定义序列化接口的概念.功能.定义及使用方法,需要的朋友可以参考下 本文实例讲述了PHP ...

最新文章

  1. scrapy和selenium结合抓取动态网页
  2. [HDOJ3652]B-Number(数位dp)
  3. Python 技术篇-用request库调用莉莉机器人api接口实现与机器人对话实例演示
  4. java恶意小程序_小程序java实现校验一张图片是否含有违法违规内容security.imgSecCheck...
  5. new Random().Next(1, 100); 多线程同时执行结果很高概率相同,
  6. HarmonyOS之常用组件Text的功能和使用
  7. js字符串与数组的处理
  8. Google、Facebook、GitHub、Babel核心成员齐聚,第13届D2前端技术论坛正式启动
  9. 第 三 十 八 天:Linux 的 LVM 逻 辑 卷 管 理
  10. Linux QT5.12 一种整体界面字体设置的方法及设置PlainTextEdit组件的字体大小方法
  11. axure 元件_在Axure中实现波纹点击特效按钮的方法
  12. javascript window Timing
  13. 支付宝解释 2019 年账单总额较高;腾讯 QQ 回应新功能可显示对方实时电量;Python 2.7 结束支持 | 极客头条...
  14. ccfb类会议有哪些_CCF推荐国际学术会议
  15. h5网站模板_超全超实用的80个模板网站,我全部整理在这里了
  16. 【土壤分类】基于支持向量机实现土壤分类附matlab代码
  17. Struts2项目实战 微云盘(五):核心功能实现
  18. 街区最短路径问题——曼哈顿距离
  19. 不小心把文件夹管理员权限删除的回复方法
  20. 计算机刷新定义,刷新计数器

热门文章

  1. 压力测试中 4 个常见面试题总结
  2. c语言如何采集plc上的数据处理,PLC数据采集的方法小结
  3. 三菱FX PLC 数据采集 【MD8口】
  4. 铁通,痛在哪里?(二)
  5. 同一个局域网怎么传文件
  6. 华为交换机设置vlan
  7. WineQQ2012 最新下载 (转)
  8. 卷积神经网络——24位彩色图像的卷积的详细介绍
  9. GCJ经纬度(腾讯、高德)转WGS84经纬度EXCEL算法
  10. 电子设计教程43:流水灯电路-非对称式多谐振荡器