Skip to content

Commit

Permalink
Add cachedKey in local calendar cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fansaien committed Jan 30, 2019
1 parent 1bda4a9 commit 241e360
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
23 changes: 16 additions & 7 deletions widgets/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,6 @@ public function prepareQuery($startTime = 0, $endTime = 0)
}
}

/**
* Current month date start_time end_time
*/
if ($startTime >0) $innerQuery->whereRaw($this->recordEnd .' >= ?', [Carbon::createFromTimestamp($startTime)]);
if ($endTime > 0) $innerQuery->whereRaw($this->recordStart . ' < ?', [Carbon::createFromTimestamp($endTime)]);

});

/*
Expand Down Expand Up @@ -599,15 +593,18 @@ public function prepareQuery($startTime = 0, $endTime = 0)
*
* Create a MD5 string based on current query SQL
* to set the cacheKey in calendar cache
*
* @see MemoryCache->hash()
*
* @param QueryBuilder $query
* @return string md5
*/
protected function getCacheKey($query){

$bindings = array_map(function ($binding) {
return (string)$binding;
}, $query->getBindings());

$name = $query->getConnection()->getName();
$md5 = md5($name . $query->toSql() . serialize($bindings));
return $md5;
Expand All @@ -626,6 +623,16 @@ public function getRecords($startTime = 0 , $endTime = 0)
$query = $this->prepareQuery($startTime, $endTime);
$cacheKey = $this->getCacheKey($query);

/**
* The $startTime and $endTime are from calendar month, should be ignore
*/
if ($startTime > 0 || $endTime > 0){
$query->where(function ($innerQuery) use ($startTime, $endTime) {
if ($startTime > 0) $innerQuery->whereRaw($this->recordEnd .' >= ?', [Carbon::createFromTimestamp($startTime)]);
if ($endTime > 0) $innerQuery->whereRaw($this->recordStart . ' < ?', [Carbon::createFromTimestamp($endTime)]);
});
}

$records = $query->get();
$list = [];

Expand All @@ -641,7 +648,9 @@ public function getRecords($startTime = 0 , $endTime = 0)
}
return [
'events' => $list,
'cacheKey' => $cacheKey
'cacheKey' => $cacheKey,
'startTime' => $startTime,
'endTime' => $endTime,
];
}

Expand Down
36 changes: 16 additions & 20 deletions widgets/calendar/assets/js/october.calendar.cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CalendarCache {
constructor(firstDay = 0, capcity = 12) {
this.cache = [];
this.lfuCache = [];
this.lastCacheEvents = null;
this.cacheKey = '0';
this.lastMonthReqeustData = null;
this.length = 0;
this.firstDay = firstDay;
Expand Down Expand Up @@ -44,6 +44,7 @@ class CalendarCache {

clearCache() {
this.length = 0;
this.cacheKey = '0';
this.lfuCache = [];
this.cache = [];
}
Expand Down Expand Up @@ -83,7 +84,8 @@ class CalendarCache {
for (let key in this.cache) {
let element = this.cache[key];
const timeKeys = key.split('-');
if (startTime >= parseInt(timeKeys[0]) && endTime <= parseInt(timeKeys[1])) {
if (this.cacheKey === timeKeys[0] &&
startTime >= parseInt(timeKeys[1]) && endTime <= parseInt(timeKeys[2])) {
self.incrLFUCount(key);
results = element;
break;
Expand Down Expand Up @@ -129,28 +131,27 @@ class CalendarCache {

}

getMonthRequestDataString() {
if (this.lastMonthReqeustData === null) return '';
const requestString = 'calendar_start_time=' + this.lastMonthReqeustData.startTime +
'&calendar_end_time=' + this.lastMonthReqeustData.endTime +
'&calendar_time_zone=' + encodeURI(this.lastMonthReqeustData.timeZone);
return requestString;
}

getLastMonthRequestData() {
return this.lastMonthReqeustData;
}

saveCache(monthData, events) {
saveCache(monthData, data) {
const events = data.events;
const startTime = monthData.startTime;
const endTime = monthData.endTime;
const key = startTime + '-' + endTime;
const key = data.cacheKey + '-' + startTime + '-' + endTime;
this.cache[key] = events;
this.setCacheKey(data.cacheKey);
this.length++;
this.incrLFUCount(key);
this.removeOldCache();
}

setCacheKey(cacheKey = '0') {
this.cacheKey = cacheKey;
}

showIndicator() {
if (this.showIndicatorCallback) this.showIndicatorCallback();
}
Expand All @@ -163,18 +164,15 @@ class CalendarCache {

let events = this.getCacheData(requestData);
if (events !== null) {
if (events !== this.lastCacheEvents) {
this.lastCacheEvents = events;
this.lastMonthReqeustData = requestData;
onSuccessCallback(events);
}
this.lastMonthReqeustData = requestData;
onSuccessCallback(events);
return;
}

this.showIndicator();

const monthData = this.getMonthRequestData(requestData);
// TODO month Data need to send to the server to refresh the session start_time and end_time

const self = this;

$.request(methodName, {
Expand All @@ -183,8 +181,7 @@ class CalendarCache {
const events = data.events;
self.hideIndicator();
// the events is whole month data
self.saveCache(monthData, events);
self.lastCacheEvents = events;
self.saveCache(monthData, data);
onSuccessCallback(events);
},
error: function (jqXHR, textStatus, error) {
Expand All @@ -199,6 +196,5 @@ class CalendarCache {
this._showIndicatorCallback = null;
this.cache = [];
this.lfuCache = [];
this.lastCacheEvents = null;
}
}
5 changes: 5 additions & 0 deletions widgets/calendar/assets/js/october.calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@
&& data.hasOwnProperty('method')) {
if (data.id === 'calendar' && data.method === 'onRefresh') {
this.clearEvents();
const requestData = {
startTime: data.startTime,
endTime: data.endTime
}
this.calendarCache.saveCache(requestData, data);
this.addEvents(data.events);
}
}
Expand Down

0 comments on commit 241e360

Please sign in to comment.