@@ -32,13 +32,15 @@ final class DefaultCacheHolder {
3232 private final ServerCacheOptions queryDefault ;
3333 private final CurrentTenantProvider tenantProvider ;
3434 private final QueryCacheEntryValidate queryCacheEntryValidate ;
35+ private final boolean tenantPartitionedCache ;
3536
3637 DefaultCacheHolder (CacheManagerOptions builder ) {
3738 this .cacheFactory = builder .getCacheFactory ();
3839 this .beanDefault = builder .getBeanDefault ();
3940 this .queryDefault = builder .getQueryDefault ();
4041 this .tenantProvider = builder .getCurrentTenantProvider ();
4142 this .queryCacheEntryValidate = builder .getQueryCacheEntryValidate ();
43+ this .tenantPartitionedCache = builder .isTenantPartitionedCache ();
4244 }
4345
4446 void visitMetrics (MetricVisitor visitor ) {
@@ -56,16 +58,42 @@ ServerCache getCache(Class<?> beanType, String collectionProperty) {
5658 return getCacheInternal (beanType , ServerCacheType .COLLECTION_IDS , collectionProperty );
5759 }
5860
61+ private String key (String beanName ) {
62+ if (tenantPartitionedCache ) {
63+ StringBuilder sb = new StringBuilder (beanName .length () + 64 );
64+ sb .append (beanName );
65+ sb .append ('.' );
66+ sb .append (tenantProvider .currentId ());
67+ return sb .toString ();
68+ } else {
69+ return beanName ;
70+ }
71+ }
72+
5973 private String key (String beanName , ServerCacheType type ) {
60- return beanName + type .code ();
74+ StringBuilder sb = new StringBuilder (beanName .length () + 64 );
75+ sb .append (beanName );
76+ if (tenantPartitionedCache ) {
77+ sb .append ('.' );
78+ sb .append (tenantProvider .currentId ());
79+ }
80+ sb .append (type .code ());
81+ return sb .toString ();
6182 }
6283
6384 private String key (String beanName , String collectionProperty , ServerCacheType type ) {
85+ StringBuilder sb = new StringBuilder (beanName .length () + 64 );
86+ sb .append (beanName );
87+ if (tenantPartitionedCache ) {
88+ sb .append ('.' );
89+ sb .append (tenantProvider .currentId ());
90+ }
6491 if (collectionProperty != null ) {
65- return beanName + "." + collectionProperty + type .code ();
66- } else {
67- return beanName + type .code ();
92+ sb .append ('.' );
93+ sb .append (collectionProperty );
6894 }
95+ sb .append (type .code ());
96+ return sb .toString ();
6997 }
7098
7199 /**
@@ -82,12 +110,17 @@ private ServerCache createCache(Class<?> beanType, ServerCacheType type, String
82110 if (type == ServerCacheType .COLLECTION_IDS ) {
83111 lock .lock ();
84112 try {
85- collectIdCaches .computeIfAbsent (beanType .getName (), s -> new ConcurrentSkipListSet <>()).add (key );
113+ collectIdCaches .computeIfAbsent (key ( beanType .getName () ), s -> new ConcurrentSkipListSet <>()).add (key );
86114 } finally {
87115 lock .unlock ();
88116 }
89117 }
90- return cacheFactory .createCache (new ServerCacheConfig (type , key , shortName , options , tenantProvider , queryCacheEntryValidate ));
118+ if (tenantPartitionedCache ) {
119+ return cacheFactory .createCache (new ServerCacheConfig (type , key , shortName , options , null , queryCacheEntryValidate ));
120+ } else {
121+ return cacheFactory .createCache (new ServerCacheConfig (type , key , shortName , options , tenantProvider , queryCacheEntryValidate ));
122+ }
123+
91124 }
92125
93126 void clearAll () {
@@ -103,7 +136,7 @@ public void clear(String name) {
103136 clearIfExists (key (name , ServerCacheType .QUERY ));
104137 clearIfExists (key (name , ServerCacheType .BEAN ));
105138 clearIfExists (key (name , ServerCacheType .NATURAL_KEY ));
106- Set <String > keys = collectIdCaches .get (name );
139+ Set <String > keys = collectIdCaches .get (key ( name ) );
107140 if (keys != null ) {
108141 for (String collectionIdKey : keys ) {
109142 clearIfExists (collectionIdKey );
@@ -147,4 +180,7 @@ private ServerCacheOptions getBeanOptions(Class<?> cls) {
147180 return beanDefault .copy (nearCache );
148181 }
149182
183+ boolean isTenantPartitionedCache () {
184+ return tenantPartitionedCache ;
185+ }
150186}
0 commit comments