Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions app/lib/account/consent_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:_pub_shared/data/account_api.dart' as api;
import 'package:clock/clock.dart';
import 'package:gcloud/service_scope.dart' as ss;
import 'package:logging/logging.dart';
import 'package:pub_dev/shared/redis_cache.dart';
import 'package:retry/retry.dart';

import '../account/agent.dart';
Expand Down Expand Up @@ -118,19 +119,31 @@ class ConsentBackend {
kind: kind,
args: args,
);
final query = _db.query<Consent>()..filter('dedupId =', dedupId);
final list = await query.run().toList();
if (list.isNotEmpty) {
final old = list.first;
if (old.isExpired()) {
Consent? existing;

final dedupCacheEntry = cache.consentDedupLookup(dedupId);
final cachedConsentId = await dedupCacheEntry.get();
if (cachedConsentId != null) {
final key = _db.emptyKey.append(Consent, id: cachedConsentId);
existing = await _db.lookupOrNull<Consent>(key);
}
if (existing == null) {
final query = _db.query<Consent>()
..filter('dedupId =', dedupId)
..limit(1);
final list = await query.run().toList();
existing = list.singleOrNull;
}
if (existing != null) {
if (existing.isExpired()) {
// expired entries should be deleted
await _delete(old, (a) => a.onExpire(old));
} else if (old.shouldNotify()) {
await _delete(existing, (a) => a.onExpire(existing!));
} else if (existing.shouldNotify()) {
// non-expired entries just re-send the notification
return await _sendNotification(activeAgent.displayId, old);
return await _sendNotification(activeAgent.displayId, existing);
} else {
return api.InviteStatus(
emailSent: false, nextNotification: old.nextNotification);
emailSent: false, nextNotification: existing.nextNotification);
}
}
// Create a new entry.
Expand All @@ -144,6 +157,7 @@ class ConsentBackend {
consent,
auditLogRecord,
]);
await dedupCacheEntry.set(consent.consentId);
return await _sendNotification(activeAgent.displayId, consent);
});
}
Expand Down
8 changes: 7 additions & 1 deletion app/lib/shared/redis_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import '../../../service/rate_limit/models.dart';
import '../../../service/security_advisories/models.dart';
import '../../dartdoc/models.dart';
import '../../shared/env_config.dart';
import '../account/models.dart' show LikeData, SessionData;
import '../account/models.dart' show Consent, LikeData, SessionData;
import '../package/models.dart' show PackageView;
import '../publisher/models.dart' show PublisherPage;
import '../scorecard/models.dart' show ScoreCardData;
Expand Down Expand Up @@ -58,6 +58,12 @@ class CachePatterns {
decode: (d) => SessionData.fromJson(d as Map<String, dynamic>),
))[sessionId];

/// Cache for [Consent] mapping the `dedupId` to the `consentId`.
Entry<String> consentDedupLookup(String dedupId) => _cache
.withPrefix('consent-dedup-lookup/')
.withTTL(Duration(minutes: 5))
.withCodec(utf8)[dedupId];

Entry<String> uiPackagePage(String package, String? version) => _cache
.withPrefix('ui-packagepage/')
.withTTL(Duration(minutes: 10))
Expand Down