Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache layer #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .theme/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/cache*
7 changes: 5 additions & 2 deletions .theme/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jQuery(function($) {
var html_class = settings.color + ' ' + settings.font;
var current_path = document.location.pathname.substring(settings.root.length);

function typeAheadInit() {
function typeAheadInit( cache = false ) {
var search_data = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.nonword('name'),
queryTokenizer: Bloodhound.tokenizers.nonword,
Expand All @@ -39,7 +39,8 @@ jQuery(function($) {
data: {
root_path : settings.root,
current_path : document.location.pathname,
depth : settings.depth
depth : settings.depth,
cache: cache
},
success: function() {
$search.removeClass('loading');
Expand Down Expand Up @@ -307,6 +308,8 @@ jQuery(function($) {

initTheme();
updateTheme();
typeAheadInit('true');
typeAheadEvents();
typeAheadInit();
typeAheadEvents();
serverLinksGenerator();
Expand Down
22 changes: 18 additions & 4 deletions .theme/lib/directory-crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,23 @@ function remove_root_path( $directory ) {
$root_path = !empty( $_GET[ 'root_path' ] ) ? $_GET[ 'root_path' ] : null;
$current_path = !empty( $_GET[ 'current_path' ] ) ? $_GET[ 'current_path' ] : null;
$depth = !empty( $_GET[ 'depth' ] ) ? $_GET[ 'depth' ] : 0;
$use_cache = !empty( $_GET['cache'] ) ? $_GET['cache'] : '/';

$crawler = new DirectoryCrawler( $root_path, $current_path, $depth );
$directories = $crawler->get_directories();
$directories_json = json_encode( $directories );
$cache_filename = 'cache-' . urlencode( $root ) . '-' . urlencode( $path ) . '-' . urlencode( $depth ) . '.txt';

die( $directories_json );
if ( $use_cache ) {
$cache = file_get_contents( $cache_filename );
if ( empty( $cache ) ) {
$cache = '';
}

die( $cache );
} else {
$crawler = new DirectoryCrawler( $root_path, $current_path, $depth );
$directories = $crawler->get_directories();
$directories_json = json_encode( $directories );

file_put_contents( $cache_filename, $directories_json );

die( $directories_json );
}