Skip to content

Instantly share code, notes, and snippets.

@ktpm489
Forked from dsternlicht/feeds.model.js
Created August 5, 2019 08:00
Show Gist options
  • Save ktpm489/d83afbfbe44d8e6a8dbda5622c2b27a3 to your computer and use it in GitHub Desktop.
Save ktpm489/d83afbfbe44d8e6a8dbda5622c2b27a3 to your computer and use it in GitHub Desktop.
getFeedById 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 = {
// ...
getFeedById(feedID) {
const selectQuery = `SELECT * FROM feeds WHERE feedID = ${feedID}`;
return cache.get(feedID, () => DB.then((connection) =>
connection.query(selectQuery).then((rows) => {
return rows[0];
})
)).then((result) => {
return result;
});
},
// ...
};
export default FeedModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment