当前位置: 首页 > news >正文

Fory序列化与反序列化

你好呀,我的老朋友!我是老寇,跟我一起学习Fory序列化与反序列化

介绍

Fory官方地址

Apache Fory是一个速度极快的多语言序列化框架,由JIT(即时编译)和零拷贝提供支持,可提供高达 170 倍的性能和极致的易用性

Apache Fory 之前名为 Apache Fury。对于 0.11 之前的版本,请在包名称、导入和依赖项中使用“fury”而不是“fory”

注意:这篇文章主要用来记录之前写的代码,便于后面翻阅查看!!!

实践

依赖

<dependency><groupId>org.apache.fory</groupId><artifactId>fory-core</artifactId><version>0.12.0</version>
</dependency>

使用

public final class ForyFactory {public static final ForyFactory INSTANCE = new ForyFactory();private final ThreadSafeFory fory = new ForyBuilder().withLanguage(Language.JAVA)// enable reference tracking for shared/circular reference.// Disable it will have better performance if no duplicate reference..withRefTracking(false)// compress int for smaller size// .withIntCompressed(true)// compress long for smaller size// .withLongCompressed(true).withCompatibleMode(CompatibleMode.SCHEMA_CONSISTENT)// enable type forward/backward compatibility// disable it for small size and better performance.// .withCompatibleMode(CompatibleMode.COMPATIBLE)// enable async multi-threaded compilation..withAsyncCompilation(true).requireClassRegistration(true).buildThreadSafeFory();public <T> void register(Class<T> clazz) {fory.register(clazz);}public byte[] serialize(Object object) {if (object == null) {return new byte[0];}if (object instanceof String str) {return str.getBytes(StandardCharsets.UTF_8);}return fory.serialize(object);}public Object deserialize(byte[] bytes) {if (bytes == null) {return null;}return fory.deserialize(bytes);}}

Mybatis-Plus本地缓存SQL

在这里插入图片描述
我们使用fory进行序列化,替换掉原有的jdk序列化

public class ForySerialCaffeineJsqlParseCache extends AbstractCaffeineJsqlParseCache {static {ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Alias.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Alias.AliasColumn.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.AllValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.AnalyticExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.AnyComparisonExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ArrayConstructor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ArrayExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.CaseExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.CastExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.CollateExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ConnectByRootOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.DateTimeLiteralExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.DateValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.DoubleValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.ExtractExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.FilterOverImpl.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Function.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.HexValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.IntervalExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JdbcNamedParameter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JdbcParameter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonAggregateFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonFunctionExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.JsonKeyValuePair.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.KeepExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.LongValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.MySQLGroupConcat.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.MySQLIndexHint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NextValExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NotExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NullValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.NumericBind.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OracleHierarchicalExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OracleHint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OracleNamedFunctionParameter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OrderByClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.OverlapsCondition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.PartitionByClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.RangeExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.RowConstructor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.RowGetExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.SQLServerHints.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.SignedExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.StringValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimeKeyExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimeValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimestampValue.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TimezoneExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TranscodingFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.TrimFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.UserVariable.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.VariableAssignment.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WhenClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowDefinition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowElement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowOffset.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowRange.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.XMLSerializeExpr.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Addition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseAnd.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseLeftShift.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseOr.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseRightShift.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.BitwiseXor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Concat.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Division.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.IntegerDivision.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Modulo.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.conditional.AndExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.conditional.OrExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.conditional.XorExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.Between.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ContainedBy.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.Contains.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.DoubleAnd.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.EqualsTo.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ExistsExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ExpressionList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.FullTextSearch.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.GeometryDistance.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.GreaterThan.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.InExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.IsBooleanExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.IsDistinctExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.IsNullExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.JsonOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.LikeExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.Matches.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.MemberOfExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.MinorThan.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.NamedExpressionList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.SimilarToExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.TSQLLeftJoin.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.TSQLRightJoin.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.parser.ASTNodeAccessImpl.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.parser.Token.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Column.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Sequence.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Synonym.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Table.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.Block.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.Commit.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.DeclareStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.DeclareStatement.TypeDefExpr.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.DescribeStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ExplainStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ExplainStatement.Option.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.IfElseStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.OutputClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.PurgeStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ReferentialAction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ResetStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.RollbackStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.SavepointStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.SetStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ShowColumnsStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.ShowStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.Statements.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.UnsupportedStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.UseStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.Alter.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDataType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDropDefault.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterExpression.ColumnDropNotNull.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterSession.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.AlterSystemStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.RenameTableStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.alter.sequence.AlterSequence.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.analyze.Analyze.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.comment.Comment.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.function.CreateFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.index.CreateIndex.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.procedure.CreateProcedure.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.schema.CreateSchema.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.sequence.CreateSequence.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.synonym.CreateSynonym.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.CheckConstraint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ColDataType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ColumnDefinition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.CreateTable.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ExcludeConstraint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.ForeignKeyIndex.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.Index.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.Index.ColumnParams.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.NamedConstraint.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.table.RowMovement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.view.AlterView.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.create.view.CreateView.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.delete.Delete.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.drop.Drop.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.execute.Execute.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.grant.Grant.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.Insert.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.InsertConflictAction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.InsertConflictTarget.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.Merge.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.MergeDelete.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.MergeInsert.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.merge.MergeUpdate.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.AllColumns.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.AllTableColumns.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Distinct.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ExceptOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Fetch.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.First.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ForClause.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.GroupByElement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.IntersectOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Join.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.KSQLJoinWindow.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.KSQLWindow.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.LateralSubSelect.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.LateralView.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Limit.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.MinusOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Offset.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.OptimizeFor.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.OrderByElement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ParenthesedFromItem.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ParenthesedSelect.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Pivot.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.PivotXml.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.PlainSelect.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SelectItem.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SetOperationList.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SetOperationList.SetOperationType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Skip.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.TableFunction.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.TableStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Top.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.UnPivot.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.UnionOp.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Values.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Wait.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.WithIsolation.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.WithItem.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.show.ShowIndexStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.show.ShowTablesStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.truncate.Truncate.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.update.Update.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.update.UpdateSet.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.upsert.Upsert.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.util.cnfexpression.MultiAndExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.util.cnfexpression.MultiOrExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.BinaryExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.ComparisonOperator.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.OldOracleJoinBinaryExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.Function.NullHandling.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.CreateFunctionalStatement.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.Select.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.SetOperation.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.util.cnfexpression.MultipleExpression.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.InsertModifierPriority.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.OrderByElement.NullOrdering.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ForMode.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.MySqlSqlCacheFlags.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.PlainSelect.BigQuerySelectQualifier.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.update.UpdateModifierPriority.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.operators.relational.LikeExpression.KeyWord.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.delete.DeleteModifierPriority.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.schema.Partition.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.insert.ConflictActionType.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.ForClause.ForOption.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.KSQLWindow.TimeUnit.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.statement.select.First.Keyword.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowElement.Type.class);ForyFactory.INSTANCE.register(net.sf.jsqlparser.expression.WindowOffset.Type.class);}public ForySerialCaffeineJsqlParseCache(Cache<String, byte[]> cache) {super(cache);}@Overridepublic byte[] serialize(Object obj) {return ForyFactory.INSTANCE.serialize(obj);}@Overridepublic Object deserialize(String sql, byte[] bytes) {return ForyFactory.INSTANCE.deserialize(bytes);}}
static {JsqlParserGlobal.setJsqlParseCache(new ForySerialCaffeineJsqlParseCache(Caffeine.newBuilder().maximumSize(4096).expireAfterWrite(10, TimeUnit.MINUTES).build()));
}

Redis序列化

jackson序列化

public final class GlobalJsonJacksonCodec extends JsonJacksonCodec {/*** 实例.*/public static final GlobalJsonJacksonCodec INSTANCE = new GlobalJsonJacksonCodec();private GlobalJsonJacksonCodec() {super(objectMapper());}// @formatter:off/*** 解决查询缓存转换异常的问题.* @return ObjectMapper*/private static ObjectMapper objectMapper() {ObjectMapper objectMapper = new ObjectMapper();DateTimeFormatter dateTimeFormatter = DateUtils.getDateTimeFormatter(DateUtils.YYYY_B_MM_B_DD_HH_R_MM_R_SS);JavaTimeModule javaTimeModule = new JavaTimeModule();// Long类型转String类型javaTimeModule.addSerializer(Long.class, ToStringSerializer.instance);javaTimeModule.addSerializer(Long.TYPE, ToStringSerializer.instance);// LocalDateTimejavaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter));javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));// InstantjavaTimeModule.addSerializer(Instant.class, new CustomInstantSerializer(InstantSerializer.INSTANCE, false,false, dateTimeFormatter));javaTimeModule.addDeserializer(Instant.class, new CustomInstantDeserializer(InstantDeserializer.INSTANT, dateTimeFormatter));objectMapper.registerModule(javaTimeModule);// 所有属性访问器(字段、getter和setter),将自动检测所有字段属性objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 对于所有非final类型,使用LaissezFaire子类型验证器来推断类型objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, NON_FINAL, JsonTypeInfo.As.PROPERTY);// 反序列化时,属性不存在的兼容处理objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);// 自动查找并注册相关模块objectMapper.findAndRegisterModules();return objectMapper;}// @formatter:onpublic static Jackson2JsonRedisSerializer<Object> getJsonRedisSerializer() {// Json序列化配置return new Jackson2JsonRedisSerializer<>(objectMapper(), Object.class);}}
/*** 自定义RedisTemplate.* @param lettuceConnectionFactory 工厂* @return RedisTemplate*/
@Bean("redisTemplate")
@ConditionalOnMissingBean(RedisTemplate.class)
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(lettuceConnectionFactory);Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = getJsonRedisSerializer();// string序列化StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();// keyredisTemplate.setKeySerializer(stringRedisSerializer);// valueredisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash-keyredisTemplate.setHashKeySerializer(stringRedisSerializer);// hash-valueredisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);// 初始化redisTemplate.afterPropertiesSet();return redisTemplate;
}@Bean("reactiveRedisTemplate")
@ConditionalOnMissingBean(ReactiveRedisTemplate.class)
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {Jackson2JsonRedisSerializer<Object> jsonRedisSerializer = getJsonRedisSerializer();StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext.<String, Object>newSerializationContext().key(stringRedisSerializer).value(jsonRedisSerializer).hashKey(stringRedisSerializer).hashValue(jsonRedisSerializer).build();return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}

使用fory序列化,替换掉原来jackson

public final class ForyRedisSerializer implements RedisSerializer<Object> {@Overridepublic byte[] serialize(Object obj) throws SerializationException {return ForyFactory.INSTANCE.serialize(obj);}@Overridepublic Object deserialize(byte[] bytes) throws SerializationException {return ForyFactory.INSTANCE.deserialize(bytes);}public static StringRedisSerializer getStringRedisSerializer() {return new StringRedisSerializer(StandardCharsets.UTF_8);}public static ForyRedisSerializer foryRedisSerializer() {// Json序列化配置return new ForyRedisSerializer();}}
@Bean("redisTemplate")
@ConditionalOnMissingBean(RedisTemplate.class)
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(lettuceConnectionFactory);// fory序列化ForyRedisSerializer foryRedisSerializer = foryRedisSerializer();// string序列化StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();// keyredisTemplate.setKeySerializer(stringRedisSerializer);// valueredisTemplate.setValueSerializer(foryRedisSerializer);// hash-keyredisTemplate.setHashKeySerializer(stringRedisSerializer);// hash-valueredisTemplate.setHashValueSerializer(foryRedisSerializer);// 初始化redisTemplate.afterPropertiesSet();return redisTemplate;
}@Bean("reactiveRedisTemplate")
@ConditionalOnMissingBean(ReactiveRedisTemplate.class)
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {// fory序列化ForyRedisSerializer foryRedisSerializer = foryRedisSerializer();// string序列化StringRedisSerializer stringRedisSerializer = getStringRedisSerializer();RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext.<String, Object>newSerializationContext().key(stringRedisSerializer).value(foryRedisSerializer).hashKey(stringRedisSerializer).hashValue(foryRedisSerializer).build();return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, serializationContext);
}

Kafka序列化

Kafka默认使用StringSerializer,因此,我们替换成fory

public class ForyKafkaDeserializer implements Deserializer<Object> {@Overridepublic Object deserialize(String s, byte[] bytes) {return ForyFactory.INSTANCE.deserialize(bytes);}}
public class ForyKafkaSerializer implements Serializer<Object> {@Overridepublic byte[] serialize(String s, Object o) {return ForyFactory.INSTANCE.serialize(o);}}
spring:kafka:consumer:# 键的序列化方式key-deserializer: org.apache.kafka.common.serialization.StringDeserializer# 值的序列化方式value-deserializer: xxx.ForyKafkaDeserializerproducer:# 键的序列化方式key-serializer: org.apache.kafka.common.serialization.StringSerializer# 值的序列化方式value-serializer: xxx.ForyKafkaSerializer

Pulsar序列化

public final class ForySchema extends AbstractSchema<Object> {public static final ForySchema INSTANCE = new ForySchema();@Overridepublic byte[] encode(Object message) {return ForyFactory.INSTANCE.serialize(message);}@Overridepublic Object decode(byte[] bytes) {return ForyFactory.INSTANCE.deserialize(bytes);}@Overridepublic SchemaInfo getSchemaInfo() {return SchemaInfo.builder().name("Fory").type(SchemaType.BYTES).schema(new byte[0]).build();}@Overridepublic Object decode(ByteBuf byteBuf) {return ForyFactory.INSTANCE.deserialize(byteBuf.array());}}
@Component
@RequiredArgsConstructor
public class PulsarMessageHandler {private final ReactivePulsarTemplate<Object> reactivePulsarTemplate;public Mono<MessageId> handle() {return reactivePulsarTemplate.send("xxx", null, ForySchema.INSTANCE);}}

我是老寇,我们下次再见啦!

http://www.dtcms.com/a/350406.html

相关文章:

  • 以正确方式构建AI Agents:Agentic AI的设计原则
  • 技术速递|使用 AI 应用模板扩展创建一个 .NET AI 应用与自定义数据进行对话
  • 【Hadoop】HDFS 分布式存储系统
  • Nuxt.js@4 中管理 HTML <head> 标签
  • 【二叉树 - LeetCode】236. 二叉树的最近公共祖先
  • TAISAW钛硕|TST嘉硕Differential output Crystal Oscillator - TW0692AAAE40
  • [electron]开发环境驱动识别失败
  • 深度学习篇--- ResNet-18
  • ArXiv 每日论文追踪器:自动检索、双语总结、邮件推送、一键建站
  • QML 中 的 Row 和 RowLayout
  • (一)C#基础(异步)
  • 数字图像处理(二)
  • 面向机器人推动与抓取任务自适应算法研究
  • langchain的简单应用案例---(2)使用Memory实现一个带记忆的对话机器人
  • 工作记录 2015-10-29
  • 销售额和营业收入的区别在哪?哪个值应该更大一些?
  • 新项目,如何做成本估算?
  • 本地缓存与 Redis 缓存的区别与实际应用
  • 【OpenAI】ChatGPT-4o-latest 真正的多模态、长文本模型的详细介绍+API的使用教程!
  • 2025软件测试面试题(持续更新)
  • 07-JUnit测试
  • ubuntu 卡到登录页面进不去--实测
  • 陪护系统有哪些功能?
  • 高并发内存池(4)-TLS:Thread Local Storage
  • Vue.nextTick讲解
  • kubectl 客户端访问 Kubernetes API Server 不通的原因排查与解决办法
  • 800G时代!全场景光模块矩阵解锁数据中心超高速未来
  • AR眼镜赋能矿业冶金数字化转型
  • Wireshark笔记-DHCP流程与数据包解析
  • Linux驱动开发笔记(七)——并发与竞争(上)——原子操作