Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: dart/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/index/file/CachingNodeManager.java

Issue 371913004: Version 1.5.6 (Closed) Base URL: http://dart.googlecode.com/svn/branches/1.5/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2014, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express
11 * or implied. See the License for the specific language governing permissions a nd limitations under
12 * the License.
13 */
14
15 package com.google.dart.engine.internal.index.file;
16
17 import com.google.common.cache.Cache;
18 import com.google.common.cache.CacheBuilder;
19 import com.google.common.util.concurrent.Uninterruptibles;
20 import com.google.dart.engine.context.AnalysisContext;
21
22 import java.util.concurrent.TimeUnit;
23
24 /**
25 * A {@link NodeManager} that caches {@link IndexNode} accessed from another {@l ink NodeManager}.
26 * <p>
27 * By default up to 64 nodes are cached for 5 seconds.
28 *
29 * @coverage dart.engine.index
30 */
31 public class CachingNodeManager implements NodeManager {
32 /**
33 * A {@link Thread} that performs {@link #cache} clean up.
34 */
35 private class CleanUpThread extends Thread {
36 public CleanUpThread() {
37 super("IndexNode cache clean up thread");
38 setDaemon(true);
39 }
40
41 @Override
42 public void run() {
43 while (true) {
44 cache.cleanUp();
45 Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
46 }
47 }
48 }
49
50 private final NodeManager manager;
51 private final Cache<String, IndexNode> cache;
52
53 public CachingNodeManager(NodeManager manager) {
54 this.manager = manager;
55 {
56 CacheBuilder<Object, Object> builder = CacheBuilder.newBuilder();
57 cache = builder.maximumSize(64).expireAfterAccess(5, TimeUnit.SECONDS).bui ld();
58 }
59 new CleanUpThread().start();
60 }
61
62 @Override
63 public void clear() {
64 manager.clear();
65 cache.invalidateAll();
66 }
67
68 @Override
69 public ContextCodec getContextCodec() {
70 return manager.getContextCodec();
71 }
72
73 @Override
74 public ElementCodec getElementCodec() {
75 return manager.getElementCodec();
76 }
77
78 @Override
79 public int getLocationCount() {
80 return manager.getLocationCount();
81 }
82
83 @Override
84 public IndexNode getNode(String name) {
85 IndexNode node = cache.getIfPresent(name);
86 if (node == null) {
87 node = manager.getNode(name);
88 if (node != null) {
89 cache.put(name, node);
90 }
91 }
92 return node;
93 }
94
95 @Override
96 public StringCodec getStringCodec() {
97 return manager.getStringCodec();
98 }
99
100 @Override
101 public IndexNode newNode(AnalysisContext context) {
102 return manager.newNode(context);
103 }
104
105 @Override
106 public void putNode(String name, IndexNode node) {
107 cache.put(name, node);
108 manager.putNode(name, node);
109 }
110
111 @Override
112 public void removeNode(String name) {
113 cache.invalidate(name);
114 manager.removeNode(name);
115 }
116 }
OLDNEW
« no previous file with comments | « no previous file | dart/editor/tools/plugins/com.google.dart.engine/src/com/google/dart/engine/internal/index/file/ContextCodec.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698