Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Mattias J Duffy
schedules
Commits
a7aa16ad
Unverified
Commit
a7aa16ad
authored
Jul 23, 2016
by
Mark Stenglein
Browse files
Added caching to large semester transfer
parent
f7518241
Changes
4
Hide whitespace changes
Inline
Side-by-side
schedules/app.js
View file @
a7aa16ad
...
...
@@ -9,7 +9,7 @@ var mongoose = require('mongoose');
var
config
=
require
(
'
./config
'
);
var
routes
=
require
(
'
./routes/index
'
);
var
api
=
require
(
'
./routes/api
'
);
//
var api = require('./routes/api');
var
app
=
express
();
...
...
schedules/config.js
View file @
a7aa16ad
...
...
@@ -7,7 +7,7 @@ config.siteDescription = "A simple application to add your class schedule to cal
// MongoDB config
config
.
mongoDBURL
=
'
mongodb://localhost/myappdatabase
'
;
config
.
ReloadDB
=
tru
e
;
config
.
ReloadDB
=
fals
e
;
config
.
schoolSlugs
=
{
// Slug: Long Name
...
...
schedules/routes/api.js
deleted
100644 → 0
View file @
f7518241
var
express
=
require
(
'
express
'
);
var
mongoose
=
require
(
'
mongoose
'
);
var
Semester
=
require
(
'
../models/Semester
'
);
var
api
=
express
.
Router
();
// Load site wide configurations
var
config
=
require
(
'
../config
'
);
/* GET Schools listing. */
api
.
get
(
'
/api/lists/schools
'
,
function
(
req
,
res
,
next
)
{
res
.
send
(
JSON
.
stringify
(
schools
));
});
// GET ical file
api
.
get
(
'
/api/json/:SEMSLUG
'
,
function
(
req
,
res
)
{
var
slug
=
req
.
params
[
'
SEMSLUG
'
];
// find each person with matching slug
var
query
=
Semester
.
findOne
({
'
slug
'
:
slug
});
query
.
exec
(
function
(
err
,
semester
)
{
if
(
err
||
(
!
semester
))
{
res
.
json
({
'
results
'
:
'
error, try something different
'
})
}
else
{
res
.
json
(
semester
);}
})
res
.
send
(
crnNums
);
});
module
.
exports
=
api
;
schedules/routes/index.js
View file @
a7aa16ad
...
...
@@ -3,7 +3,6 @@ var router = express.Router();
var
mongoose
=
require
(
'
mongoose
'
);
var
Semester
=
require
(
'
../models/Semester
'
);
// Load site wide configurations
var
config
=
require
(
'
../config
'
);
...
...
@@ -12,15 +11,49 @@ router.get('/docs', function(req, res, next) {
})
// GET ical file
router
.
get
(
'
/api/json/:SEMSLUG
'
,
function
(
req
,
res
)
{
var
jsonSlugCache
=
{
slugs
:
[]
};
router
.
get
(
'
/api/json/classes/:SEMSLUG
'
,
function
(
req
,
res
)
{
var
slug
=
req
.
params
[
'
SEMSLUG
'
];
// find each person with matching slug
var
query
=
Semester
.
findOne
({
'
slug
'
:
slug
});
query
.
exec
(
function
(
err
,
semester
)
{
if
(
err
||
(
!
semester
))
{
res
.
json
({
'
results
'
:
'
error, try something different
'
})
}
else
{
res
.
json
(
semester
);}
})
if
(
jsonSlugCache
[
slug
])
{
jsonSlugCache
[
slug
].
timesUsed
++
;
console
.
log
(
jsonSlugCache
[
slug
].
timesUsed
)
console
.
log
(
jsonSlugCache
[
slug
].
tiemsUsed
);
res
.
send
(
jsonSlugCache
[
slug
][
'
value
'
]);
}
else
{
// find each person with matching slug
var
query
=
Semester
.
findOne
({
'
slug
'
:
slug
});
query
.
exec
(
function
(
err
,
semester
)
{
if
(
err
||
(
!
semester
))
{
res
.
json
({
'
results
'
:
'
error, try something different
'
})
}
else
{
if
(
jsonSlugCache
.
slugs
.
length
>
10
)
{
var
leastUsedAmount
=
jsonSlugCache
[
jsonSlugCache
.
slugs
[
0
]].
timesUsed
;
var
leastUsed
=
0
;
for
(
var
i
=
0
;
i
<
jsonSlugCache
.
slugs
.
length
;
i
++
)
{
var
amountUsed
=
jsonSlugCache
[
jsonSlugCache
.
slugs
[
i
]].
timesUsed
;
if
(
amountUsed
<
leastUsedAmount
)
{
leastUsedAmount
=
amountUsed
;
leastUsed
=
i
;
}
}
delete
jsonSlugCache
[
jsonSlugCache
.
slugs
[
leastUsed
]];
jsonSlugCache
.
slugs
.
splice
(
leastUsed
,
1
);
}
console
.
log
(
JSON
.
stringify
(
semester
))
jsonSlugCache
[
slug
]
=
{
'
timesUsed
'
:
1
}
jsonSlugCache
[
slug
].
value
=
JSON
.
stringify
(
semester
);
jsonSlugCache
.
slugs
.
push
(
slug
);
res
.
send
(
jsonSlugCache
[
slug
].
value
);
}
})
}
});
// TODO: Actually learn how to make this function set be passed around properly
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment