Skip to content

Commit

Permalink
fix(cache): Fix more memory provider cache object reconstruction issues
Browse files Browse the repository at this point in the history
#123

Same issues as a949a4e -- memory provider stores objects in memory (no serialization) so need to check for object instance before trying to reconstruct
  • Loading branch information
FoxxMD committed Oct 26, 2022
1 parent 3622170 commit d318286
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Common/Infrastructure/Reddit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Comment, Submission} from "snoowrap/dist/objects";
import {Comment, RedditUser, Submission, Subreddit} from "snoowrap/dist/objects";
import { ValueOf } from "ts-essentials";

export type ActivityType = 'submission' | 'comment';
Expand Down Expand Up @@ -33,6 +33,8 @@ type valueof<T> = T[keyof T]
* we don't know which they are until we retrieve them.
* */
export type SnoowrapLike = Record<keyof SnoowrapActivity, valueof<SnoowrapActivity>>;
export type RedditUserLike = Record<keyof RedditUser, valueof<RedditUser>>;
export type SubredditLike = Record<keyof Subreddit, valueof<Subreddit>>;

export interface CachedFetchedActivitiesResult {
pre: SnoowrapActivity[] | SnoowrapLike[]
Expand Down
21 changes: 15 additions & 6 deletions src/Subreddit/SubredditResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ import {
ActivityType,
AuthorHistorySort,
CachedFetchedActivitiesResult,
FetchedActivitiesResult,
FetchedActivitiesResult, RedditUserLike,
SnoowrapActivity,
SubredditLike,
SubredditRemovalReason
} from "../Common/Infrastructure/Reddit";
import {AuthorCritPropHelper} from "../Common/Infrastructure/Filters/AuthorCritPropHelper";
Expand Down Expand Up @@ -737,10 +738,13 @@ export class SubredditResources {
if (this.ttl.subredditTTL !== false) {

hash = `sub-${subName}`;
const cachedSubreddit = await this.cache.get(hash);
const cachedSubreddit = await this.cache.get(hash) as Subreddit | SubredditLike | null | undefined;
if (cachedSubreddit !== undefined && cachedSubreddit !== null) {
logger.debug(`Cache Hit: Subreddit ${subName}`);
await this.subredditStats.incrementCacheTypeStat('subreddit', hash, false);
if(cachedSubreddit instanceof Subreddit) {
return cachedSubreddit;
}
return new Subreddit(cachedSubreddit, this.client, false);
}
await this.subredditStats.incrementCacheTypeStat('subreddit', hash, true);
Expand Down Expand Up @@ -885,11 +889,13 @@ export class SubredditResources {
const hash = `authorModNotes-${subredditName}-${authorName}`;

if (this.ttl.modNotesTTL !== false) {
const cachedModNoteData = await this.cache.get(hash) as ModNoteRaw[] | null | undefined;
const cachedModNoteData = await this.cache.get(hash) as ModNoteRaw[] | ModNote[] | null | undefined;
if (cachedModNoteData !== undefined && cachedModNoteData !== null) {
this.logger.debug(`Cache Hit: Author ModNotes ${authorName} in ${subredditName}`);

return cachedModNoteData.map(x => {
if(x instanceof ModNote) {
return x;
}
const note = new ModNote(x, this.client);
note.subreddit = this.subreddit;
if (val instanceof RedditUser) {
Expand Down Expand Up @@ -934,7 +940,7 @@ export class SubredditResources {

if (this.ttl.modNotesTTL !== false) {
const hash = `authorModNotes-${this.subreddit.display_name}-${data.user.name}`;
const cachedModNoteData = await this.cache.get(hash) as ModNoteRaw[] | null | undefined;
const cachedModNoteData = await this.cache.get(hash) as ModNoteRaw[] | ModNote[] | null | undefined;
if (cachedModNoteData !== undefined && cachedModNoteData !== null) {
this.logger.debug(`Adding new Note ${newNote.id} to Author ${data.user.name} Note cache`);
await this.cache.set(hash, [newNote, ...cachedModNoteData], {ttl: this.ttl.modNotesTTL});
Expand All @@ -952,9 +958,12 @@ export class SubredditResources {
}
const hash = `author-${authorName}`;
if (this.ttl.authorTTL !== false) {
const cachedAuthorData = await this.cache.get(hash);
const cachedAuthorData = await this.cache.get(hash) as RedditUser | RedditUserLike | null | undefined;
if (cachedAuthorData !== undefined && cachedAuthorData !== null) {
this.logger.debug(`Cache Hit: Author ${authorName}`);
if(cachedAuthorData instanceof RedditUser) {
return cachedAuthorData;
}
const {subreddit, ...rest} = cachedAuthorData as any;
const snoowrapConformedData = {...rest};
if(subreddit !== null) {
Expand Down

0 comments on commit d318286

Please sign in to comment.