Skip to content

Commit

Permalink
Added onCacheDataDoesNotAlreadyExist event
Browse files Browse the repository at this point in the history
  • Loading branch information
ChronSyn committed Oct 17, 2020
1 parent cc6889f commit cbf0f50
Show file tree
Hide file tree
Showing 20 changed files with 490 additions and 78 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ The changes made should drastically simplify working with the library and provid
* A new method, `deleteCacheData()`, has been added to allow deletion of any cached data by providing it's field name / key
* The concept of expired and non-expired data no longer exists - Cache data now automatically deletes when it expires
* New optional callbacks in the constructor method have been aded: `onCacheDataAdd`, `onCacheDataDelete`, `onCacheDataExpired`, `onCacheDataAlreadyExists`
* Version 2.1.0 has added a new optional callback: `onCacheDataAccessed` - this will trigger when a field is accessed in the cache
* Version 2.1.2 has added some new optional callback:
* `onCacheDataAccessed` - this will trigger when a field is accessed in the cache
* `onCacheDataDoesNotAlreadyExist` - this will trigger if the specified field is not found in the cache when calling `setCacheData`, `deleteCacheData`
* Enabling and disabling debug has been removed due to addition of callbacks
* You can now provide a name to your cache, which can be useful for the callbacks
* Methods to allow changing the cache age and name have been added
Expand Down
10 changes: 10 additions & 0 deletions build/quicache.lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface ICacheConstructorProps {
onCacheDataDelete?: (data: IOnCacheEvent) => void;
onCacheDataExpired?: (data: IOnCacheEvent) => void;
onCacheDataAlreadyExists?: (data: IOnCacheEvent) => void;
onCacheDataDoesNotAlreadyExist?: (data: IOnCacheDataNotExistEvent) => void;
onCacheNameSet?: (data: IOnCacheNameSet) => void;
onCacheMaxAgeSet?: (data: IOnCacheMaxAgeSet) => void;
}
Expand All @@ -57,6 +58,14 @@ interface IOnCacheMaxAgeSet {
oldMaxAgeInSeconds: number;
newMaxAgeInSeconds: number;
}
interface IOnCacheDataNotExistEvent {
/** The key used to map data in the cache */
field: string | number;
/** The cache name as defined during construction */
cacheName: string;
/** The time until the data with the specified field/key will expire, in seconds */
expires: number;
}
interface IOnCacheEvent {
/** The key used to map data in the cache */
field: string | number;
Expand Down Expand Up @@ -92,6 +101,7 @@ declare class CacheManager implements ICacheManager {
private _onCacheDataAccessed;
private _onCacheDataDelete;
private _onCacheDataAlreadyExists;
private _onCacheDataDoesNotAlreadyExist;
private _onCacheNameSet;
private _onCacheMaxAgeSet;
constructor(args: ICacheConstructorProps);
Expand Down
30 changes: 25 additions & 5 deletions build/quicache.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var CacheManager = /** @class */ (function () {
* @public
*/
this.setCacheData = function (field, data) {
var _a, _b;
var _a, _b, _c;
if (_this.cacheDataExists(field)) {
_this._onCacheDataAlreadyExists({
data: _this._dataCache[field],
Expand All @@ -126,27 +126,39 @@ var CacheManager = /** @class */ (function () {
});
return _this === null || _this === void 0 ? void 0 : _this._dataCache[field];
}
else {
_this._onCacheDataDoesNotAlreadyExist({
cacheName: _this._cacheName,
expires: (_b = _this.getCacheDataAge(field)) !== null && _b !== void 0 ? _b : -1,
field: field
});
}
_this._dataCache[field] = {
timestamp: new Date().getTime(),
data: data
};
_this._onCacheDataAdd({
data: _this._dataCache[field],
cacheName: _this._cacheName,
expires: (_b = _this.getCacheDataAge(field)) !== null && _b !== void 0 ? _b : -1,
expires: (_c = _this.getCacheDataAge(field)) !== null && _c !== void 0 ? _c : -1,
field: field
});
var deleteTimeout = _this._cacheMaxAgeInSeconds * 1000;
setTimeout(function () {
var _a;
var _a, _b;
// Check if cached data exists before attempting to invoke expiration callback or delete non-existant property
if (!_this.cacheDataExists(field)) {
_this._onCacheDataDoesNotAlreadyExist({
cacheName: _this._cacheName,
expires: (_a = _this.getCacheDataAge(field)) !== null && _a !== void 0 ? _a : -1,
field: field
});
return null;
}
_this._onCacheDataExpired({
data: _this._dataCache[field],
cacheName: _this._cacheName,
expires: (_a = _this.getCacheDataAge(field)) !== null && _a !== void 0 ? _a : -1,
expires: (_b = _this.getCacheDataAge(field)) !== null && _b !== void 0 ? _b : -1,
field: field
});
delete _this._dataCache[field];
Expand All @@ -160,7 +172,7 @@ var CacheManager = /** @class */ (function () {
* @public
*/
this.deleteCacheData = function (field) {
var _a;
var _a, _b;
if (_this.cacheDataExists(field)) {
_this._onCacheDataDelete({
data: _this._dataCache[field],
Expand All @@ -172,6 +184,13 @@ var CacheManager = /** @class */ (function () {
delete _this._dataCache[field];
return cacheEntry;
}
else {
_this._onCacheDataDoesNotAlreadyExist({
cacheName: _this._cacheName,
expires: (_b = _this.getCacheDataAge(field)) !== null && _b !== void 0 ? _b : -1,
field: field
});
}
return null;
};
if (!(args === null || args === void 0 ? void 0 : args.cacheMaxAgeInSeconds)) {
Expand All @@ -186,6 +205,7 @@ var CacheManager = /** @class */ (function () {
this._onCacheDataAdd = function (data) { return args.onCacheDataAdd ? args.onCacheDataAdd(data) : {}; };
this._onCacheDataExpired = function (data) { return args.onCacheDataExpired ? args.onCacheDataExpired(data) : {}; };
this._onCacheDataAlreadyExists = function (data) { return args.onCacheDataAlreadyExists ? args.onCacheDataAlreadyExists(data) : {}; };
this._onCacheDataDoesNotAlreadyExist = function (data) { return args.onCacheDataDoesNotAlreadyExist ? args.onCacheDataDoesNotAlreadyExist(data) : {}; };
this._onCacheDataAccessed = function (data) { return args.onCacheDataAccessed ? args.onCacheDataAccessed(data) : {}; };
this._onCacheDataDelete = function (data) { return args.onCacheDataDelete ? args.onCacheDataDelete(data) : {}; };
this._onCacheNameSet = function (data) { return args.onCacheNameSet ? args.onCacheNameSet(data) : {}; };
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/js/search.json

Large diffs are not rendered by default.

Loading

0 comments on commit cbf0f50

Please sign in to comment.