This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Cache latest versions in Redis #50
Closed
hiroppy
wants to merge
1
commit into
typings:master
from
hiroppy:feature/set-latest-version-in-redis
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import redis = require('redis') | ||
import Promise = require('any-promise') | ||
|
||
const client = redis.createClient() | ||
|
||
export function getVersion (source: string, name: string): Promise<any> { | ||
const key = `${source}:${name}` | ||
|
||
return new Promise((resolve: Function, reject: Function) => { | ||
client.get(key, (err, data) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
resolve(data) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ import Promise = require('any-promise') | |
import semver = require('semver') | ||
import pad = require('pad-left') | ||
import db from '../../../support/knex' | ||
import { | ||
setVersion as setVersionToRedis, | ||
deleteVersion as deleteVersionInRedis | ||
} from './redis' | ||
|
||
export interface UpsertOptions { | ||
table: string | ||
|
@@ -12,9 +16,17 @@ export interface UpsertOptions { | |
trx?: knex.Transaction | ||
where?: string | ||
returning?: string[] | ||
redisKey?: string | ||
} | ||
|
||
export function upsert (options: UpsertOptions): Promise<Object> { | ||
if (options.table === 'versions') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't so it this way. I'd only have jobs mark things as invalid (E.g. delete entries), but I'd make the front-end APIs handing caching. This way the entire project doesn't need to be re-indexed to update the cache, we just mark things as invalid (or delete them, in this case). |
||
setVersionToRedis({ | ||
redisKey: options.redisKey, | ||
insert: options.insert | ||
}) | ||
} | ||
|
||
const insert = db(options.table) | ||
.insert(options.insert) | ||
.transacting(options.trx) | ||
|
@@ -47,6 +59,7 @@ export interface VersionOptions { | |
version: string | ||
compiler?: string | ||
location?: string | ||
redisKey?: string | ||
} | ||
|
||
export interface EntryOptions { | ||
|
@@ -105,7 +118,8 @@ export function createVersion (options: VersionOptions): Promise<{ id: string }> | |
updates: ['version', 'location', 'updated', 'compiler', 'deprecated'], | ||
conflicts: ['entry_id', 'tag'], | ||
returning: ['id'], | ||
where: 'versions.updated <= excluded.updated' | ||
where: 'versions.updated <= excluded.updated', | ||
redisKey: options.redisKey | ||
}) | ||
.then((row: any) => { | ||
if (row) { | ||
|
@@ -123,6 +137,7 @@ export function createVersion (options: VersionOptions): Promise<{ id: string }> | |
|
||
export function createEntryAndVersion (options: EntryAndVersionOptions): Promise<{ id: string }> { | ||
const { name, source, updated, version, compiler, location } = options | ||
const redisKey = `${source}:${name}` | ||
|
||
return createEntry(options) | ||
.then((row) => { | ||
|
@@ -140,7 +155,8 @@ export function createEntryAndVersion (options: EntryAndVersionOptions): Promise | |
updated, | ||
version, | ||
compiler, | ||
location | ||
location, | ||
redisKey | ||
}) | ||
}) | ||
} | ||
|
@@ -154,25 +170,28 @@ export interface VersionsOptions { | |
export function deleteVersions (options: VersionsOptions) { | ||
const { name, source, updated } = options | ||
|
||
return db.transaction(trx => { | ||
return db('entries') | ||
.transacting(trx) | ||
.first('id') | ||
.where({ name, source }) | ||
.then((row) => { | ||
if (row == null) { | ||
return | ||
} | ||
|
||
return db('versions') | ||
.transacting(trx) | ||
.update({ deprecated: updated }) | ||
.where('entry_id', '=', row.id) | ||
.andWhere('updated', '<', updated) | ||
.returning('id') | ||
return deleteVersionInRedis(source, name) | ||
.then(() => { | ||
return db.transaction(trx => { | ||
return db('entries') | ||
.transacting(trx) | ||
.first('id') | ||
.where({ name, source }) | ||
.then((row) => { | ||
if (row == null) { | ||
return | ||
} | ||
|
||
return db('versions') | ||
.transacting(trx) | ||
.update({ deprecated: updated }) | ||
.where('entry_id', '=', row.id) | ||
.andWhere('updated', '<', updated) | ||
.returning('id') | ||
}) | ||
.then(trx.commit) | ||
.catch(trx.rollback) | ||
}) | ||
.then(trx.commit) | ||
.catch(trx.rollback) | ||
}) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import _ = require('lodash') | ||
import redis = require('redis') | ||
import Promise = require('any-promise') | ||
|
||
const client = redis.createClient() | ||
|
||
export function setVersion (options: any) { | ||
const option = _.clone(options.insert) | ||
delete option.entry_id | ||
|
||
client.set(options.redisKey, JSON.stringify(option)) | ||
} | ||
|
||
export function deleteVersion (source: string, name: string) { | ||
return new Promise((resolve) => { | ||
client.del(`${source}:${name}`, () => resolve()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work with the version operator. If it's in Redis, searching by version will break.