You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/07/25 12:04:49 UTC

[GitHub] [superset] michael-s-molina commented on a diff in pull request #20842: feat: a simple LRUCache in frontend

michael-s-molina commented on code in PR #20842:
URL: https://github.com/apache/superset/pull/20842#discussion_r928775729


##########
superset-frontend/packages/superset-ui-core/test/utils/lruCache.test.ts:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+import { lruCache } from '@superset-ui/core';
+
+describe('simple LRUCache', () => {

Review Comment:
   [Avoid nesting when you're testing](https://github.com/apache/superset/wiki/Testing-Guidelines-and-Best-Practices#avoid-nesting-when-youre-testing)



##########
superset-frontend/packages/superset-ui-core/test/utils/lruCache.test.ts:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+import { lruCache } from '@superset-ui/core';
+
+describe('simple LRUCache', () => {
+  test('initial LRU', () => {
+    expect(lruCache().capacity).toBe(100);
+    expect(lruCache(10).capacity).toBe(10);
+    expect(lruCache(10).size).toBe(0);
+    expect(() => lruCache(0)).toThrow(Error);
+  });
+
+  test('LRU', () => {

Review Comment:
   Can we break this into multiple tests? We could have one to test eviction, one to test invalid key types, and one to test cache clearing.



##########
superset-frontend/packages/superset-ui-core/src/utils/lruCache.ts:
##########
@@ -0,0 +1,70 @@
+/**
+ * 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.
+ */
+
+class LRUCache<T> {
+  private cache: Map<string, T>;
+
+  readonly capacity: number;
+
+  constructor(capacity: number) {
+    if (capacity < 1) {
+      throw new Error('The capacity in LRU must be greater than 0.');
+    }
+    this.capacity = capacity;
+    this.cache = new Map<string, T>();
+  }
+
+  public has(key: string): boolean {
+    return this.cache.has(key);
+  }
+
+  public get(key: string): T | undefined {
+    if (this.cache.has(key)) {
+      const tmp = this.cache.get(key) as T;
+      this.cache.delete(key);
+      this.cache.set(key, tmp);
+      return tmp;
+    }
+

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org