diff --git advagg.module advagg.module
index 834dfe1..86a96d1 100755
--- advagg.module
+++ advagg.module
@@ -646,10 +646,7 @@ function advagg_processor(&$variables) {
   }
 
   // http or https.
-  $schema = (    (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
-              || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
-              || (isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] == 'on')
-                ) ? 'https' : 'http';
+  $schema = advagg_get_current_schema();
 
   // CSS
   $css_var = $variables['css'];
@@ -809,7 +806,8 @@ function advagg_get_filename($files, $filetype, $counter = FALSE, $bundle_md5 =
   $run_alter = FALSE;
   if (empty($bundle_md5)) {
     // Create bundle md5
-    $bundle_md5 = md5(implode('', $files));
+    $schema = advagg_get_current_schema();
+    $bundle_md5 = md5($schema . implode('', $files));
     $run_alter = TRUE;
 
     // Record root request in db.
@@ -981,7 +979,8 @@ function advagg_find_existing_bundle(&$files, &$bundle_md5) {
   // Sort files for better cache hits.
   $temp_files = $files;
   sort($temp_files);
-  $cached_data_key = 'advagg_existing_' . md5(implode('', $temp_files));
+  $schema = advagg_get_current_schema();
+  $cached_data_key = 'advagg_existing_' . md5($schema . implode('', $temp_files));
 
   // Try cache first; cache table is cache_advagg_bundle_reuse. string is debug name.
   $cached_data = advagg_cached_bundle_get($cached_data_key, 'advagg_find_existing_bundle');
@@ -2378,8 +2377,9 @@ function advagg_css_js_file_builder($type, $files, $query_string = '', $counter
   global $_advagg, $base_path;
   $data = '';
 
+  $schema = advagg_get_current_schema();
   // Try cache first. When ever the counter changes this cache gets reset.
-  $cached_data_key = 'advagg_file_builder_' . md5(implode('', array_filter(array_unique($files))));
+  $cached_data_key = 'advagg_file_builder_' . md5($schema . implode('', array_filter(array_unique($files))));
   if (!$force) {
     // Try cache first; cache table is cache_advagg_bundle_reuse.
     $cached_data = advagg_cached_bundle_get($cached_data_key, 'file_builder_cache_object');
@@ -2548,7 +2548,7 @@ function advagg_css_js_file_builder($type, $files, $query_string = '', $counter
 
   // Cache the output
   if (!$force && $cacheable) {
-    $new_cached_data_key = 'advagg_file_builder_' . md5(implode('', array_filter(array_unique($files_used))));
+    $new_cached_data_key = 'advagg_file_builder_' . md5($schema . implode('', array_filter(array_unique($files_used))));
     // Verify the files in equals the files out.
     if ($new_cached_data_key == $cached_data_key) {
       cache_set($cached_data_key, $output, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
@@ -3582,3 +3582,21 @@ function _advagg_build_url($filepath = '') {
 
   return 'http://' . $auth . $ip . $port . $base_path . $filepath;
 }
+
+/**
+ * Returns the schema of current page
+ *
+ * @return string 'http' or 'https'
+ */
+function advagg_get_current_schema() {
+  static $schema = NULL;
+
+  if (!isset($schema)) {
+    $schema = (    (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
+              || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
+              || (isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] == 'on')
+                ) ? 'https' : 'http';
+  }
+
+  return $schema;
+}
diff --git advagg_js_cdn/advagg_js_cdn.module advagg_js_cdn/advagg_js_cdn.module
index 6790ab4..1b69ba9 100755
--- advagg_js_cdn/advagg_js_cdn.module
+++ advagg_js_cdn/advagg_js_cdn.module
@@ -59,10 +59,8 @@ function advagg_js_cdn_advagg_js_pre_alter(&$javascript, $preprocess_js, $public
   }
 
   // http or https.
-  $schema = (  (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on')
-            || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
-            || (isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] == 'on')
-              ) ? 'https' : 'http';
+  $schema = advagg_get_current_schema();
+
   foreach ($javascript as $type => $data) {
     // Skip inline and setting js.
     if (!$data || $type == 'setting' || $type == 'inline') {
