mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-12-01 04:34:07 +01:00
Merge branch 'master' into v3
This commit is contained in:
commit
1ed62858dd
3 files changed
+126
-12
No files matched your search
@@ -108,4 +108,82 @@ describe('ApiCacheManager', () => {
|
||||
expect(cache.get(key)).to.be.undefined
|
||||
})
|
||||
})
|
||||
|
||||
describe('in-flight invalidation', () => {
|
||||
it('should not cache response when full clear happens while request is in flight', () => {
|
||||
const lruCache = new LRUCache({ max: 10 })
|
||||
req.url = '/libraries/abc-123/personalized?include=rssfeed,share'
|
||||
res.getHeaders.returns({ 'content-type': 'application/json' })
|
||||
manager = new ApiCacheManager(lruCache)
|
||||
|
||||
manager.middleware(req, res, next)
|
||||
manager.clear({ name: 'libraryItem' }, 'afterCreate')
|
||||
res.send('stale personalized data')
|
||||
|
||||
const key = JSON.stringify({ user: req.user.username, url: req.url })
|
||||
expect(lruCache.get(key)).to.be.undefined
|
||||
expect(res.originalSend.calledOnceWith('stale personalized data')).to.be.true
|
||||
})
|
||||
|
||||
it('should not cache personalized or recent-episodes response when mediaProgress updates while in flight', () => {
|
||||
const lruCache = new LRUCache({ max: 10 })
|
||||
req.url = '/libraries/abc-123/personalized'
|
||||
res.getHeaders.returns({ 'content-type': 'application/json' })
|
||||
manager = new ApiCacheManager(lruCache)
|
||||
|
||||
manager.middleware(req, res, next)
|
||||
manager.clear({ name: 'mediaProgress' }, 'afterUpdate')
|
||||
res.send('stale personalized data')
|
||||
|
||||
const key = JSON.stringify({ user: req.user.username, url: req.url })
|
||||
expect(lruCache.get(key)).to.be.undefined
|
||||
expect(res.originalSend.calledOnceWith('stale personalized data')).to.be.true
|
||||
})
|
||||
|
||||
it('should still cache unrelated endpoints when a high-churn slice clears while in flight', () => {
|
||||
const lruCache = new LRUCache({ max: 10 })
|
||||
req.url = '/libraries/abc-123/stats'
|
||||
res.getHeaders.returns({ 'content-type': 'application/json' })
|
||||
manager = new ApiCacheManager(lruCache)
|
||||
|
||||
manager.middleware(req, res, next)
|
||||
manager.clear({ name: 'session' }, 'afterCreate')
|
||||
res.send('fresh stats')
|
||||
|
||||
const key = JSON.stringify({ user: req.user.username, url: req.url })
|
||||
expect(lruCache.get(key)).to.deep.equal({
|
||||
body: 'fresh stats',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
statusCode: 200
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getModelName', () => {
|
||||
it('should prefer instance constructor name over name column value', () => {
|
||||
class author {
|
||||
constructor(name) {
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
const lruCache = new LRUCache({ max: 10 })
|
||||
const key = JSON.stringify({ user: 'u', url: '/libraries/abc-123/stats' })
|
||||
lruCache.set(key, { body: '{}', headers: {}, statusCode: 200 })
|
||||
manager = new ApiCacheManager(lruCache)
|
||||
|
||||
// An author named "session" must still clear the full cache, not be treated as the high-churn session model
|
||||
const instance = new author('session')
|
||||
expect(manager.getModelName(instance)).to.equal('author')
|
||||
manager.clear(instance, 'afterCreate')
|
||||
expect(lruCache.get(key)).to.be.undefined
|
||||
})
|
||||
|
||||
it('should resolve model name from arrays and bulk hook options', () => {
|
||||
class libraryItem {}
|
||||
manager = new ApiCacheManager(cache)
|
||||
|
||||
expect(manager.getModelName([new libraryItem()])).to.equal('libraryItem')
|
||||
expect(manager.getModelName({ model: { name: 'mediaProgress' } })).to.equal('mediaProgress')
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user