在我的应用程序中,我编写了一个方法来检查用户是否有权查看特定页面。在每次访问页面时,都会调用此方法,如果用户没有权限,则会将其发送回主页。
问题是,这种方法减慢了速度。这需要时间来检查并在应用程序中的页面之间移动非常慢。这是方法:
#include <errno.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
FILE *fp, *fpc;
int ch, last;
if (argc != 3) {
printf("usage: enquote filetocopy filetowrite\n");
exit(1);
}
fp = fopen(argv[1], "r");
if (!fp) {
fprintf(stderr, "Could not open input file: (%d) %s\n",
errno, strerror(errno));
return 2;
}
fpc = fopen(argv[2], "w");
if (!fpc) {
fprintf(stderr, "Could not open output file: (%d) %s\n",
errno, strerror(errno));
return 2;
}
last = '\n'; // we are at the beginning of a line
while ((ch = fgetc(fp)) != EOF) {
if (last == '\n') {
fputc('"', fpc); // " at the beginning of a line
}
if (ch == '\n') {
fputc('"', fpc); // " at the end of a line
}
fputc(ch, fpc);
last = ch;
}
if (last != '\n') {
// special case: file does not end with a \n
fputc('"', fpc); // " at the end of a line
fputc('\n', fpc); // put a \n at the end of the output file
}
fclose(fp);
fclose(fpc);
return 0;
}
以下是我如何称呼该方法:
'users.havePermission'(currentRoute) {
check(currentRoute, String);
const loggedInUser = Meteor.user();
const permissions = {
home: [ 'superAdmin', 'adviser', 'admin' ],
'page.1': [ 'superAdmin', 'adviser' ],
'page.2': [ 'superAdmin', 'adviser', 'admin' ],
'page.3': [ 'superAdmin', 'adviser', 'admin' ],
'page.4': [ 'superAdmin', 'adviser' ],
'page.5': [ 'superAdmin' ],
'page.6': [ 'superAdmin', 'adviser' ],
'page.7': [ 'superAdmin', 'admin' ],
'page.8': [ 'superAdmin', 'admin' ],
'page.9': [ 'superAdmin' ],
'page.10': [ 'superAdmin', 'adviser', 'admin' ],
'page.11': [ 'superAdmin' ],
'page.12': [ 'superAdmin' ],
'page.13': [ 'superAdmin', 'admin' ],
'page.14': [ 'superAdmin' ],
'page.15': [ 'superAdmin', 'admin' ],
'page.16': [ 'superAdmin' ],
'page.17': [ 'superAdmin', 'admin' ],
'page.18': [ 'superAdmin' ],
'page.19': [ 'superAdmin' ],
'page.20': [ 'superAdmin', 'adviser', 'admin' ]
};
const permissionForCurrentRoute = permissions[currentRoute];
return Roles.userIsInRole(loggedInUser, permissionForCurrentRoute);
}
奇怪的是,这种方法有时(并非所有时间)都会返回一个像Meteor.call('users.havePermission', currentRoute, (error, userHavePermission) => {
if (!userHavePermission) {
// If not, send directly to the home page without any warning.. Boom!
FlowRouter.redirect('/');
} else {
// If everything is in order, let him pass!
const userName = loggedInUser.profile.name;
const loggedInUserEmail = loggedInUser.emails[0].address;
LocalState.set('LOGGED_IN_USER_EMAIL', loggedInUserEmail);
const isAdmin = loggedInUser.roles.includes('admin');
const isSuperAdmin = loggedInUser.roles.includes('superAdmin');
onData(null, {notifications, userName, currentRoute, isAdmin, isSuperAdmin,
uuid, RouteTransition});
return clearAllNotifications;
}
});
这样可能导致延迟的错误。不知道。
我的主要问题是,是否可以通过在服务器上缓存来加快速度?我该怎么办?