Skip to content

Commit

Permalink
cache admins data
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran17 committed Jul 17, 2024
1 parent 6dd58a7 commit fdd8624
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
34 changes: 22 additions & 12 deletions core/admin-dashboard/components/admin/widget/BaseAdminTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:page="admin_pagination.current_page"
:items-per-page="admin_pagination.per_page"
:multi-sort="true"
:server-items-length="admin_pagination.total"
:server-items-length="admin_pagination.total - 1"
@update:items-per-page="tableUpdateItemsPerPage"
@update:page="tableUpdatePage"
>
Expand Down Expand Up @@ -186,6 +186,7 @@ export default {
return {
ADMIN_TYPES,
LOGIN_FAILED,
is_items_per_page_changed: false,
};
},
Expand Down Expand Up @@ -261,16 +262,29 @@ export default {
},
async tableUpdatePage(data) {
this.UPDATE_ADMIN_PAGINATION({ path: "current_page", data });
await this.GET_ADMINS_PAGINATED({
page: this.admin_pagination.current_page,
const admins = await this.GET_ADMINS_PAGINATED({
page: data,
entries_per_page: this.admin_pagination.per_page,
});
this.filterAdmins(admins);
},
tableUpdateItemsPerPage(data) {
this.UPDATE_ADMIN_PAGINATION({ path: "per_page", data });
async tableUpdateItemsPerPage(data) {
const admins = await this.GET_ADMINS_PAGINATED({
page: 1,
entries_per_page: data,
});
this.filterAdmins(admins);
},
filterAdmins(admins) {
const filtered_admins = admins.filter(
(admin) => admin._id !== this.me._id
);
this.SET_ADMINS({ data: filtered_admins });
},
},
Expand All @@ -281,11 +295,7 @@ export default {
entries_per_page: this.admin_pagination.per_page,
});
const filtered_admins = admins.filter(
(admin) => admin._id !== this.me._id
);
this.SET_ADMINS({ data: filtered_admins });
this.filterAdmins(admins);
} catch (error) {
console.error(error);
}
Expand Down
6 changes: 2 additions & 4 deletions core/admin-dashboard/store/admin/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ const actions: ActionTree<AdminState, RootState> = {

async [ActionTypes.GET_ADMINS]({ commit }, params = {}) {
const keep_in_store = get(params, "keep_in_store", true);
const new_state = get(params, "new_state", true);

const { data: admins } = await this.$axios.$get("/admin");

if (!keep_in_store) {
return admins;
}

commit(MutationTypes.SET_ADMINS, { data: admins, new_state });
commit(MutationTypes.SET_ADMINS, { data: admins });
return admins;
},

Expand Down Expand Up @@ -143,7 +142,6 @@ const actions: ActionTree<AdminState, RootState> = {
const query = get(params, "query");
const page = get(params, "page", 1);
const entries_per_page = get(params, "entries_per_page", 15);
const new_state = get(params, "new_state", true);

const query_url = new URLSearchParams();

Expand All @@ -155,7 +153,7 @@ const actions: ActionTree<AdminState, RootState> = {
`/admin/all-paginated?${query_url}`
);

commit(MutationTypes.SET_ADMINS, { data: admins, new_state });
commit(MutationTypes.SET_ADMINS, { data: admins });
commit(MutationTypes.SET_ADMIN_PAGINATION, { data: pagination });

return admins;
Expand Down
1 change: 1 addition & 0 deletions core/admin-dashboard/store/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const state = () => ({
per_page: 15,
total: 0,
total_pages: 0,
has_more: true,
},
});

Expand Down
3 changes: 2 additions & 1 deletion core/admin-dashboard/store/admin/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const mutations: MutationTree<AdminState> = {

[MutationTypes.SET_ADMINS](
state,
{ data, new_state }: { data: any[]; new_state: boolean }
{ data, new_state = true }: { data: any[]; new_state?: boolean }
) {
if (new_state) {
return (state.admins = data);
Expand Down Expand Up @@ -42,6 +42,7 @@ const mutations: MutationTree<AdminState> = {
total_pages: number;
from: number;
to: number;
has_more: boolean;
};
}
) {
Expand Down
14 changes: 11 additions & 3 deletions core/server/src/use-cases/admin/get-admin-analystics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Logger } from "winston";
import { RandomCacheTime } from "../../config/random-cache-time/make-random-cache-time";
import Redis from "../../config/redis";
import IAdminDb, {
IAdminAnalyticsData,
} from "../../data-access/interfaces/admin-db";
import Redis from "../../config/redis";
import { Logger } from "winston";
import { AdminType } from "../../database/interfaces/admin";

export interface IGetAdminAnalysticsPayload {
Expand All @@ -19,10 +20,12 @@ export type GetAdminAnalystics = ({

export default function makeGetAdminAnalystics({
adminDb,
randomCacheTime,
redis,
logger,
}: {
adminDb: IAdminDb;
randomCacheTime: RandomCacheTime;
redis: Redis;
logger: Logger;
}): GetAdminAnalystics {
Expand All @@ -46,10 +49,15 @@ export default function makeGetAdminAnalystics({
const data = await adminDb.getAdminAnalystics({ range, unit, author_type });

const one_day_in_seconds = 24 * 60 * 60;
const duration_in_seconds = randomCacheTime({
seconds: one_day_in_seconds,
extra_minutes: 12,
});

redis.setData({
key: cache_key,
value: data,
duration_in_seconds: one_day_in_seconds,
duration_in_seconds,
});

return data;
Expand Down
45 changes: 44 additions & 1 deletion core/server/src/use-cases/admin/get-admins-paginated.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Logger } from "winston";
import { RandomCacheTime } from "../../config/random-cache-time/make-random-cache-time";
import Redis from "../../config/redis";
import IAdminDb, {
IPaginatedAdminsResult,
} from "../../data-access/interfaces/admin-db";
Expand All @@ -16,10 +19,50 @@ export type GetAdminsPaginated = ({

export default function makeGetAdminsPaginated({
adminDb,
randomCacheTime,
redis,
logger,
}: {
adminDb: IAdminDb;
randomCacheTime: RandomCacheTime;
redis: Redis;
logger: Logger;
}): GetAdminsPaginated {
return async function getAdminsPaginated({ query, page, entries_per_page }) {
return await adminDb.findAllPaginated({ query, page, entries_per_page });
const cache_key = redis.cacheKeyBuilder({
prefix: "getAdminsPaginated",
query,
page,
entries_per_page,
});

const cached_data = <IPaginatedAdminsResult>(
await redis.getData({ key: cache_key })
);

if (cached_data) {
logger.verbose("Redis: Data found in cache", { cache_key });
return cached_data;
}

const admins = await adminDb.findAllPaginated({
query,
page,
entries_per_page,
});

const one_hour_in_seconds = 60 * 60;
const duration_in_seconds = randomCacheTime({
seconds: one_hour_in_seconds,
extra_minutes: 10,
});

redis.setData({
key: cache_key,
value: admins,
duration_in_seconds,
});

return admins;
};
}
5 changes: 5 additions & 0 deletions core/server/src/use-cases/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from "../../config/logs/logger";
import { randomCacheTime } from "../../config/random-cache-time";
import { redis } from "../../config/redis";
import { AdminDb } from "../../data-access";
import makeBatchUploadAdmins from "./batch-upload-admins";
Expand All @@ -18,6 +19,9 @@ import makeUpdateAdmin from "./update-admin";

const getAdminsPaginated = makeGetAdminsPaginated({
adminDb: AdminDb,
randomCacheTime,
redis,
logger,
});

const batchUploadAdmins = makeBatchUploadAdmins({
Expand All @@ -42,6 +46,7 @@ const getOneAdmin = makeGetOneAdmin({

const getAdminAnalystics = makeGetAdminAnalystics({
adminDb: AdminDb,
randomCacheTime,
redis,
logger,
});
Expand Down

0 comments on commit fdd8624

Please sign in to comment.