Event handling in redis in mobile development and redis in mobile development
Events in redis are classified into file events and time events. Redis uses aeEventLoop to record the event state typedef struct aeEventLoop {// file event aeFileEvent * events;/* Registered events * // time event aeTimeEvent * timeEventHead;} aeEventLoop; the file events and time events we mentioned are also stored as the structure member variables in aeEventLoop. The Structure Variables for saving file events are as follows: typedef struct aeFileEvent {// listen to the Event Type mask, int mask;/* one of AE _ (READABLE | WRITABLE) * // The callback function of the read event aeFileProc * rfileProc; // The callback function of the write event aeFileProc * wfileProc; // read and write event parameters void * clientDa Ta;} aeFileEvent; the struct of the time event is as follows: typedef struct aeTimeEvent {long id;/* time event identifier. * // The Event arrival time long when_sec;/* seconds */long when_ms;/* milliseconds * // The event processing function aeTimeProc * timeProc; aeEventFinalizerProc * finalizerProc; // void * clientData, the form parameter of the time processing function; // the pointer to the next time event, struct aeTimeEvent * next;} aeTimeEvent; redis time processing policies can be divided into the following four types # ifdef HAVE_EVPORT # include "AE _evport.c" # else # ifde F HAVE_EPOLL # include "AE _epoll.c" # else # ifdef HAVE_KQUEUE # include "AE _kqueue.c" # else # include "AE _select.c" # endif # which macro is selected during compilation?, the performance of different policies may vary. The theoretical performance ranges from large to small, namely, HAVE_EVPORT> HAVE_EPOLL> HAVE_KQUEUE> select (default) redis event processing functions are as follows: void aeMain (aeEventLoop * eventLoop) {eventLoop-> stop = 0; // visible Processing Event is an endless loop while (! EventLoop-> stop) {// before processing the time, you can call the sleep function if (eventLoop-> beforesleep! = NULL) eventLoop-> beforesleep (eventLoop); // start processing time and file event aeProcessEvents (eventLoop, AE _ALL_EVENTS);} int aeProcessEvents (aeEventLoop * eventLoop, int flags) {int processed = 0, numevents;/* Nothing to do? Return ASAP * // if there is no time or file event currently, 0 if (! (Flags & AE _TIME_EVENTS )&&! (Flags & AE _FILE_EVENTS) return 0;/* Note that we want call select () even if there are no * file events to process as long as we want to process time * events, in order to sleep until the next time event is ready * to fire. */if (eventLoop-> maxfd! =-1 | (flags & AE _TIME_EVENTS )&&! (Flags & AE _DONT_WAIT) {int j; aeTimeEvent * shortest = NULL; struct timeval TV, * tvp; // obtain the latest time event if (flags & AE _TIME_EVENTS &&! (Flags & AE _DONT_WAIT) shortest = aeSearchNearestTimer (eventLoop); if (shortest) {long now_sec, now_ms;/* Calculate the time sing misfor the nearest * timer to fire. * // obtain the current system time aeGetTime (& now_sec, & now_ms); tvp = & TV; tvp-> TV _sec = shortest-> when_sec-now_sec; // compare the difference between the time of the last event and the current system time if (shortest-> when_ms <now_ms) {tvp-> TV _usec = (shortest-> when_ms + 1000) -now_ms) * 1000; tvp-> TV _sec --;} Else {tvp-> TV _usec = (shortest-> when_ms-now_ms) * 1000;} // The difference is less than zero, indicating that the event has expired, clear the time of this time event if (tvp-> TV _sec <0) tvp-> TV _sec = 0; if (tvp-> TV _usec <0) tvp-> TV _usec = 0 ;} else {/* If we have to check for events but need to return * ASAP because of AE _DONT_WAIT we need to set the timeout * to zero */if (flags & AE _DONT_WAIT) {// set file events not to block TV. TV _sec = TV. TV _usec = 0; tvp = & TV;} else {/* Ot Herwise we can block * // file events can be blocked until an event reaches tvp = NULL; /* wait forever */} // start File Processing Event numevents = aeApiPoll (eventLoop, tvp); for (j = 0; j <numevents; j ++) {// obtain the ready file event aeFileEvent * fe = & eventLoop-> events [eventLoop-> fired [j] From the fired array. fd]; int mask = eventLoop-> fired [j]. mask; int fd = eventLoop-> fired [j]. fd; int rfired = 0;/* note the fe-> mask &... code: maybe an already proces Sed * event removed an element that fired and we still didn't * processed, so we check if the event is still valid. * // read event processing if (fe-> mask & AE _READABLE) {rfired = 1; fe-> rfileProc (eventLoop, fd, fe-> clientData, mask);} // write event processing if (fe-> mask & AE _WRITABLE) {if (! Rfired | fe-> wfileProc! = Fe-> rfileProc) fe-> wfileProc (eventLoop, fd, fe-> clientData, mask);} processed ++ ;}} /* Check time events * // execution time event if (flags & AE _TIME_EVENTS) processed + = processTimeEvents (eventLoop); return processed; /* return the number of processed file/time events */} from here, we can see that the file event takes precedence over the time event processing static int processTimeEvents (aeEventLoop * eventLoop) {int processed = 0; aeTimeEvent * te; long maxId; time_t now = t Ime (NULL); if (now <eventLoop-> lastTime) {te = eventLoop-> timeEventHead; while (te) {te-> when_sec = 0; te = te-> next ;}/// update the time when the last time event was processed, eventLoop-> lastTime = now; // The Time Series table, locate the time event te = eventLoop-> timeEventHead; maxId = eventLoop-> timeEventNextId-1; while (te) {long now_sec, now_ms; long id; // if (te-> id> maxId) {te = te-> next; continue;} // obtain the current system time aeGetTime (& now_sec ,& Now_ms ); // The Event expiration time is earlier than the current system time if (now_sec> te-> when_sec | (now_sec = te-> when_sec & now_ms >=te-> when_ms )) {int retval; id = te-> id; // callback function of the execution time event retval = te-> timeProc (eventLoop, id, te-> clientData); processed ++; // determine whether it is a scheduled event or a periodic event if (retval! = AE _NOMORE) {// periodic event, update the next expiration time of this event aeAddMillisecondsToNow (retval, & te-> when_sec, & te-> when_ms );} else {// Delete aeDeleteTimeEvent (eventLoop, id);} te = eventLoop-> timeEventHead;} else {te = te-> next ;}} return processed ;}