Skip to content

Instantly share code, notes, and snippets.

@dsternlicht
Last active January 5, 2021 10:24
Show Gist options
  • Save dsternlicht/3407e5d02e20e323c81c7cd007b523a3 to your computer and use it in GitHub Desktop.
Save dsternlicht/3407e5d02e20e323c81c7cd007b523a3 to your computer and use it in GitHub Desktop.
Feeds model with cache
// Feeds model after cache
import DB from '../db';
import CacheService from '../cache.service';
const ttl = 60 * 60 * 1; // cache for 1 Hour
const cache = new CacheService(ttl); // Create a new cache service instance
const FeedModel = {
getUserFeeds(userID) {
const selectQuery = `SELECT * FROM feeds WHERE userID = ${userID}`;
const key = `getUserFeeds_${userID}`;
return cache.get(key, () => DB.then((connection) =>
connection.query(selectQuery).then((rows) => {
return rows;
})
)).then((result) => {
return result;
});
},
getFeedById(feedID) {
const selectQuery = `SELECT * FROM feeds WHERE feedID = ${feedID}`;
const key = `getFeedById_${feedID}`;
return cache.get(key, () => DB.then((connection) =>
connection.query(selectQuery).then((rows) => {
return rows[0];
})
)).then((result) => {
return result;
});
},
countFeeds(userID) {
const selectQuery = `SELECT COUNT(*) as count FROM feeds WHERE userID = ${userID}`;
const key = `countFeeds_${userID}`;
return cache.get(key, () => DB.then((connection) =>
connection.query(selectQuery).then((rows) => {
return rows[0].count;
})
)).then((result) => {
return result;
});
},
update(userID, feedID, feedData) {
const whereQuery = `WHERE feedID = ${feedID} AND userID = ${userID}`;
const updateQuery = `UPDATE feeds SET ? ${whereQuery}`;
return DB.then((connection) => connection.query(updateQuery, feedData).then(() => {
cache.del([`getFeedById_${feedID}`, `getUserFeeds_${userID}`]);
return true;
}));
},
delete(userID, feedID) {
const deleteQuery = `DELETE from feeds WHERE userID = ${userID} AND feedID = ${feedID}`;
return DB.then((connection) => connection.query(updateQuery, params).then(() => {
cache.del([`countFeeds_${userID}`, `getFeedById_${feedID}`, `getUserFeeds_${userID}`]);
return true;
}));
}
};
export default FeedModel;
@DeepikaGogna
Copy link

can u please share db,js file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment