This is an automated email from the ASF dual-hosted git repository.

kimmking pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 5f29088  Add cache for OrderedSPIRegistry (#9788)
5f29088 is described below

commit 5f29088bff2d541df69a887f8b081d1b8e90670d
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Mar 24 00:29:01 2021 +0800

    Add cache for OrderedSPIRegistry (#9788)
    
    * Refactor OrderedSPIRegistry
    
    * Add cache for OrderedSPIRegistry
---
 .../infra/spi/ordered/OrderedSPIRegistry.java      | 17 ++++--
 .../spi/ordered/cache/CachedOrderedServices.java   | 36 ++++++++++++
 .../spi/ordered/cache/OrderedServicesCache.java    | 66 ++++++++++++++++++++++
 3 files changed, 115 insertions(+), 4 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistry.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistry.java
index 567cb0d..bcada99 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistry.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/OrderedSPIRegistry.java
@@ -20,10 +20,13 @@ package org.apache.shardingsphere.infra.spi.ordered;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.ordered.cache.CachedOrderedServices;
+import org.apache.shardingsphere.infra.spi.ordered.cache.OrderedServicesCache;
 
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Optional;
 import java.util.TreeMap;
 
 /**
@@ -35,8 +38,8 @@ public final class OrderedSPIRegistry {
     /**
      * Get registered services by class type.
      *
-     * @param orderedSPIClass class of ordered SPI
      * @param types types
+     * @param orderedSPIClass class of ordered SPI
      * @param <T> type of ordered SPI class
      * @return registered services
      */
@@ -44,7 +47,7 @@ public final class OrderedSPIRegistry {
         Collection<T> registeredServices = 
getRegisteredServices(orderedSPIClass);
         Map<Class<?>, T> result = new 
LinkedHashMap<>(registeredServices.size(), 1);
         for (T each : registeredServices) {
-            types.stream().filter(type -> ((OrderedSPI<?>) 
each).getTypeClass() == type).forEach(type -> result.put(type, each));
+            types.stream().filter(type -> each.getTypeClass() == 
type).forEach(type -> result.put(type, each));
         }
         return result;
     }
@@ -52,18 +55,24 @@ public final class OrderedSPIRegistry {
     /**
      * Get registered services.
      *
-     * @param orderedSPIClass class of ordered SPI
      * @param types types
+     * @param orderedSPIClass class of ordered SPI
      * @param <K> type of key
      * @param <V> type of ordered SPI class
      * @return registered services
      */
+    @SuppressWarnings("unchecked")
     public static <K, V extends OrderedSPI<?>> Map<K, V> 
getRegisteredServices(final Collection<K> types, final Class<V> 
orderedSPIClass) {
+        Optional<CachedOrderedServices> cachedServices = 
OrderedServicesCache.findCachedServices(types, orderedSPIClass);
+        if (cachedServices.isPresent()) {
+            return (Map<K, V>) cachedServices.get().getServices();
+        }
         Collection<V> registeredServices = 
getRegisteredServices(orderedSPIClass);
         Map<K, V> result = new LinkedHashMap<>(registeredServices.size(), 1);
         for (V each : registeredServices) {
             types.stream().filter(type -> each.getTypeClass() == 
type.getClass()).forEach(type -> result.put(type, each));
         }
+        OrderedServicesCache.cacheServices(types, orderedSPIClass, result);
         return result;
     }
     
@@ -76,7 +85,7 @@ public final class OrderedSPIRegistry {
      */
     public static <T extends OrderedSPI<?>> Collection<T> 
getRegisteredServices(final Class<T> orderedSPIClass) {
         Map<Integer, T> result = new TreeMap<>();
-        for (T each : 
ShardingSphereServiceLoader.newServiceInstances(orderedSPIClass)) {
+        for (T each : 
ShardingSphereServiceLoader.getSingletonServiceInstances(orderedSPIClass)) {
             result.put(each.getOrder(), each);
         }
         return result.values();
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/cache/CachedOrderedServices.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/cache/CachedOrderedServices.java
new file mode 100644
index 0000000..57ac4e5
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/cache/CachedOrderedServices.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.spi.ordered.cache;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * Cached ordered services.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class CachedOrderedServices {
+    
+    private final Collection<?> types;
+    
+    private final Map<?, ?> services;
+}
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCache.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCache.java
new file mode 100644
index 0000000..d0fcd2f
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/spi/ordered/cache/OrderedServicesCache.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.spi.ordered.cache;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.spi.ordered.OrderedSPI;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Ordered services cached.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class OrderedServicesCache {
+    
+    private static final Map<Class<?>, CachedOrderedServices> CACHED_SERVICES 
= new ConcurrentHashMap<>();
+    
+    /**
+     * Find cached services.
+     * 
+     * @param types types
+     * @param orderedSPIClass class of ordered SPI
+     * @param <K> type of key
+     * @param <V> type of ordered SPI class
+     * @return cached ordered services
+     */
+    public static <K, V extends OrderedSPI<?>> Optional<CachedOrderedServices> 
findCachedServices(final Collection<K> types, final Class<V> orderedSPIClass) {
+        return isHitCache(types, orderedSPIClass) ? 
Optional.of(CACHED_SERVICES.get(orderedSPIClass)) : Optional.empty();
+    }
+    
+    private static <K, V extends OrderedSPI<?>> boolean isHitCache(final 
Collection<K> types, final Class<V> orderedSPIClass) {
+        return CACHED_SERVICES.containsKey(orderedSPIClass) && 
CACHED_SERVICES.get(orderedSPIClass).getTypes().equals(types);
+    }
+    
+    /**
+     * Cache services.
+     * 
+     * @param types types
+     * @param orderedSPIClass class of ordered SPI
+     * @param services ordered services
+     * @param <K> type of key
+     * @param <V> type of ordered SPI class
+     */
+    public static <K, V extends OrderedSPI<?>> void cacheServices(final 
Collection<K> types, final Class<V> orderedSPIClass, final Map<K, V> services) {
+        CACHED_SERVICES.put(orderedSPIClass, new CachedOrderedServices(types, 
services));
+    }
+}

Reply via email to