Skip to content

Commit 382b78d

Browse files
kevmoomkustermann
authored andcommitted
PackageMemcache: Enable namespacing for index (#340)
And other cleanup
1 parent 90b2e3b commit 382b78d

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

app/lib/shared/package_memcache.dart

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,27 @@ abstract class UIPackageCache {
3131

3232
/// Uses a [Memache] to set/get/invalidate metadata for packages.
3333
class AppEnginePackageMemcache implements PackageCache, UIPackageCache {
34-
static const Duration EXPIRATION = const Duration(minutes: 10);
35-
static const String INDEX_PAGE_KEY = 'pub_index';
36-
static const String KEY_PREFIX = 'dart_package_json';
37-
static const String UI_KEY_PREFIX = 'dart_package_ui';
38-
39-
final Memcache memcache;
40-
final String keyPrefix;
41-
final String uiKeyPrefix;
42-
43-
AppEnginePackageMemcache(this.memcache, String namespace)
44-
: keyPrefix = (namespace == null || namespace.isEmpty)
45-
? KEY_PREFIX
46-
: 'ns_${namespace}_$KEY_PREFIX',
47-
uiKeyPrefix = (namespace == null || namespace.isEmpty)
48-
? UI_KEY_PREFIX
49-
: 'ns_${namespace}_$UI_KEY_PREFIX';
34+
static const Duration _cacheExpiration = const Duration(minutes: 10);
35+
36+
final Memcache _memcache;
37+
final String _indexKey;
38+
final String _keyPrefix;
39+
final String _uiKeyPrefix;
40+
41+
static String _prefixWithNamespace(String prefix, String namespace) =>
42+
(namespace == null || namespace.isEmpty)
43+
? prefix
44+
: 'ns_${namespace}_${prefix}';
45+
46+
AppEnginePackageMemcache(this._memcache, String namespace)
47+
: _keyPrefix = _prefixWithNamespace('package_json_', namespace),
48+
_uiKeyPrefix = _prefixWithNamespace('package_ui_', namespace),
49+
_indexKey = _prefixWithNamespace('pub_index', namespace);
5050

5151
@override
5252
Future<List<int>> getPackageData(String package) async {
53-
final result =
54-
await _ignoreErrors(memcache.get(_packageKey(package), asBinary: true));
53+
final result = await _ignoreErrors(
54+
_memcache.get(_packageKey(package), asBinary: true));
5555

5656
if (result != null)
5757
_logger.info('memcache["$package"] found');
@@ -64,14 +64,14 @@ class AppEnginePackageMemcache implements PackageCache, UIPackageCache {
6464
@override
6565
Future setPackageData(String package, List<int> data) {
6666
_logger.info('memcache["$package"] setting to new data');
67-
return _ignoreErrors(
68-
memcache.set(_packageKey(package), data, expiration: EXPIRATION));
67+
return _ignoreErrors(_memcache.set(_packageKey(package), data,
68+
expiration: _cacheExpiration));
6969
}
7070

7171
@override
7272
Future<String> getUIPackagePage(String package, String version) async {
7373
final result = await _ignoreErrors(
74-
memcache.get(_packageUIKey(package, version), asBinary: true));
74+
_memcache.get(_packageUIKey(package, version), asBinary: true));
7575

7676
if (result != null) {
7777
_logger.info('memcache["$package"] rendered UI found');
@@ -85,33 +85,33 @@ class AppEnginePackageMemcache implements PackageCache, UIPackageCache {
8585
@override
8686
Future setUIPackagePage(String package, String version, String data) async {
8787
_logger.info('memcache["$package"] setting to new rendered UI data');
88-
return _ignoreErrors(memcache.set(
88+
return _ignoreErrors(_memcache.set(
8989
_packageUIKey(package, version), UTF8.encode(data),
90-
expiration: EXPIRATION));
90+
expiration: _cacheExpiration));
9191
}
9292

9393
@override
9494
Future invalidateUIPackagePage(String package) async {
9595
_logger.info('memcache["$package"] invalidating UI data');
9696
return _ignoreErrors(Future.wait([
97-
memcache.remove(_packageUIKey(package, null)),
98-
memcache.remove(INDEX_PAGE_KEY),
97+
_memcache.remove(_packageUIKey(package, null)),
98+
_memcache.remove(_indexKey),
9999
]));
100100
}
101101

102102
@override
103103
Future invalidatePackageData(String package) {
104104
_logger.info('memcache["$package"] invalidate entry');
105105
return _ignoreErrors(Future.wait([
106-
memcache.remove(_packageKey(package)),
107-
memcache.remove(_packageUIKey(package, null)),
108-
memcache.remove(INDEX_PAGE_KEY),
106+
_memcache.remove(_packageKey(package)),
107+
_memcache.remove(_packageUIKey(package, null)),
108+
_memcache.remove(_indexKey),
109109
]));
110110
}
111111

112112
@override
113113
Future<String> getUIIndexPage() async {
114-
final result = await _ignoreErrors(memcache.get(INDEX_PAGE_KEY));
114+
final result = await _ignoreErrors(_memcache.get(_indexKey));
115115
if (result != null) {
116116
_logger.info('memcache[index-page] found rendered UI data');
117117
} else {
@@ -124,14 +124,14 @@ class AppEnginePackageMemcache implements PackageCache, UIPackageCache {
124124
Future setUIIndexPage(String content) async {
125125
_logger.info('memcache[index-page] setting to new rendered UI data');
126126
await _ignoreErrors(
127-
memcache.set(INDEX_PAGE_KEY, content, expiration: EXPIRATION));
127+
_memcache.set(_indexKey, content, expiration: _cacheExpiration));
128128
}
129129

130-
String _packageKey(String package) => '$keyPrefix$package';
130+
String _packageKey(String package) => '$_keyPrefix$package';
131131

132132
String _packageUIKey(String package, String version) {
133-
if (version == null) return '$uiKeyPrefix$package';
134-
return '$uiKeyPrefix$package**$version';
133+
if (version == null) return '$_uiKeyPrefix$package';
134+
return '$_uiKeyPrefix$package**$version';
135135
}
136136

137137
// We are ignoring any memcache errors and just return `null` in this case.

0 commit comments

Comments
 (0)