setup memcache
This commit is contained in:
@@ -7,15 +7,20 @@ import { addGameApis } from './gameservice'
|
||||
import { addCharacterApis } from './characterservice'
|
||||
import { addRushStatsApis } from './rushstatsservice'
|
||||
import { addDmApis } from './dmservice'
|
||||
import { Memcache } from './memcache'
|
||||
|
||||
var Memcached = require('memcached')
|
||||
|
||||
const app = express()
|
||||
const jsonParser = json()
|
||||
const port = 3001
|
||||
// const memcache = new memcached('localhost:11211', {})
|
||||
const memcachep = new Memcache('localhost:11211')
|
||||
|
||||
addGameApis(app, jsonParser)
|
||||
addCharacterApis(app, jsonParser)
|
||||
addRushStatsApis(app, jsonParser)
|
||||
addDmApis(app, jsonParser)
|
||||
addGameApis(app, jsonParser, memcachep)
|
||||
addCharacterApis(app, jsonParser, memcachep)
|
||||
addRushStatsApis(app, jsonParser, memcachep)
|
||||
addDmApis(app, jsonParser, memcachep)
|
||||
|
||||
app.use(express.static(__dirname + '/frontend'))
|
||||
app.use('/', (req, res) => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { database, Character, Game, Pick, App } from './db'
|
||||
import { OrderByParser, FilterParser, ParsingError } from './tokenizer'
|
||||
|
||||
export function addCharacterApis(app, jsonParser) {
|
||||
export function addCharacterApis(app, jsonParser, memcace) {
|
||||
app.get('/api/character/:characterId', async (req, res) => {
|
||||
try {
|
||||
const character = await Character.findOne({
|
||||
|
||||
@@ -2,7 +2,7 @@ import { database, Character, Game, Pick, App } from './db'
|
||||
import { OrderByParser, FilterParser, ParsingError } from './tokenizer'
|
||||
import { fn, col } from 'sequelize'
|
||||
|
||||
export function addDmApis(app, jsonParser) {
|
||||
export function addDmApis(app, jsonParser, memcache) {
|
||||
app.get('/api/dm/:dmName', async (req, res) => {
|
||||
const dmName = req.params.dmName
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { database, Character, Game, Pick, App } from './db'
|
||||
import { OrderByParser, FilterParser, ParsingError } from './tokenizer'
|
||||
|
||||
export function addGameApis(app, jsonParser) {
|
||||
export function addGameApis(app, jsonParser, memcache) {
|
||||
app.get('/api/game/:gameId', async (req, res) => {
|
||||
try {
|
||||
const game = await Game.findOne({ where: { id: req.params['gameId'] } })
|
||||
|
||||
27
backend/src/memcache.ts
Normal file
27
backend/src/memcache.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { promisify } from 'util'
|
||||
|
||||
var Memcached = require('memcached')
|
||||
|
||||
export class Memcache {
|
||||
readonly memcached
|
||||
|
||||
readonly getCallback
|
||||
readonly setCallback
|
||||
readonly replaceCallback
|
||||
|
||||
constructor(url: string) {
|
||||
this.memcached = new Memcached(url, {})
|
||||
|
||||
this.getCallback = promisify(this.memcached.get).bind(this.memcached)
|
||||
this.setCallback = promisify(this.memcached.set).bind(this.memcached)
|
||||
this.replaceCallback = promisify(this.memcached.replace).bind(this.memcached)
|
||||
}
|
||||
|
||||
async get(key: string) {
|
||||
return this.getCallback(key)
|
||||
}
|
||||
|
||||
async set(key: string, value) {
|
||||
return this.setCallback(key, value, 200)
|
||||
}
|
||||
}
|
||||
@@ -43,12 +43,21 @@ function monthIdToEndSeconds(monthId: number): number {
|
||||
return Number.MAX_SAFE_INTEGER
|
||||
}
|
||||
|
||||
export function addRushStatsApis(app, jsonParser) {
|
||||
export function addRushStatsApis(app, jsonParser, memcache) {
|
||||
app.post('/api/serverstats/gamestats', jsonParser, async (req, res) => {
|
||||
const monthId = req.body.monthId
|
||||
let startSeconds = monthIdToStartSeconds(monthId)
|
||||
let endSeconds = monthIdToEndSeconds(monthId)
|
||||
|
||||
let cachedResponse = await memcache.get('gamestats' + req.body.monthId)
|
||||
|
||||
if (cachedResponse) {
|
||||
console.info('cache hit - ' + 'gamestats' + req.body.monthId)
|
||||
res.send(cachedResponse)
|
||||
return
|
||||
}
|
||||
|
||||
console.log('cache miss')
|
||||
const serverGameStats = await database.query(serverGameStatsQuery, {
|
||||
type: QueryTypes.SELECT,
|
||||
replacements: {
|
||||
@@ -56,6 +65,8 @@ export function addRushStatsApis(app, jsonParser) {
|
||||
endSeconds
|
||||
}
|
||||
})
|
||||
|
||||
await memcache.set('gamestats' + req.body.monthId, serverGameStats[0])
|
||||
res.send(serverGameStats[0])
|
||||
})
|
||||
|
||||
@@ -64,6 +75,13 @@ export function addRushStatsApis(app, jsonParser) {
|
||||
let startSeconds = monthIdToStartSeconds(monthId)
|
||||
let endSeconds = monthIdToEndSeconds(monthId)
|
||||
|
||||
let cachedResponse = await memcache.get('rolestats' + req.body.monthId)
|
||||
|
||||
if (cachedResponse) {
|
||||
console.log('cache hit')
|
||||
res.send(cachedResponse)
|
||||
return
|
||||
}
|
||||
const games = await Game.findAll({
|
||||
include: [
|
||||
{ model: Character, as: 'characterPickedForGame' },
|
||||
@@ -110,6 +128,7 @@ export function addRushStatsApis(app, jsonParser) {
|
||||
}
|
||||
})
|
||||
|
||||
await memcache.set('rolestats' + req.body.monthId, result)
|
||||
res.send(result)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user