如何在Drupal 8中以编程方式清除缓存?

时间:2017-04-25 10:31:05

标签: caching drupal drupal-8

我正在使用Drupal 8网站。这个网站正在运作。我最近搬到了另一台机器上。

显示如下错误:

Recoverable fatal error: Argument 1 passed to 

Drupal\Component\DependencyInjection\Container::__construct() must be of

 the type array, boolean given, called in [DRUPAL-

PATH]/core/lib/Drupal/Core/DrupalKernel.php on line 883 and defined in 

[DRUPAL-PATH]/core/lib/Drupal/Component/DependencyInjection/Container.php

 on line 119 #0 [DRUPAL-PATH]/core/includes/bootstrap.inc(550): 

_drupal_error_handler_real(4096, 'Argument 1 pass...', 

'/Applications/M...', 119, Array)

我需要以编程方式清除缓存。

5 个答案:

答案 0 :(得分:6)

请尝试以下。

cache_clear_all() // For Drupal-7

drupal_flush_all_caches() // For Drupal-8

答案 1 :(得分:4)

按SQL

TRUNCATE `cache_bootstrap`;
TRUNCATE `cache_config`;
TRUNCATE `cache_container`;
TRUNCATE `cache_data`;
TRUNCATE `cache_default`;
TRUNCATE `cache_discovery`;
TRUNCATE `cache_dynamic_page_cache`;
TRUNCATE `cache_entity`;
TRUNCATE `cache_menu`;
TRUNCATE `cache_render`;
TRUNCATE `cache_rest`;
TRUNCATE `cachetags`;
TRUNCATE `cache_toolbar`;

通过Drush

drush cr all

答案 2 :(得分:2)

如果您想清除特定的缓存,例如渲染缓存,则可以运行以下代码:

    private List<TwitchClip> getVideoList() {
    try {
        LocalTime midnight = LocalTime.MIDNIGHT;
        LocalDate today = LocalDate.now(ZoneId.of("Europe/Berlin"));
        LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);

        String formattedStartTime;
        String formattedEndTime;
        if(String.valueOf(todayMidnight.getMonthValue()).length() != 2) {
            formattedStartTime = todayMidnight.getYear() + "-" + 0 + todayMidnight.getMonthValue() + "-" + (todayMidnight.getDayOfMonth() - 1) + "T00:00:00Z";
            formattedEndTime = todayMidnight.getYear() + "-" + 0 + todayMidnight.getMonthValue() + "-" + todayMidnight.getDayOfMonth() + "T00:00:00Z";
        }else {
            formattedStartTime = todayMidnight.getYear() + "-" + todayMidnight.getMonthValue() + "-" + (todayMidnight.getDayOfMonth() - 1) + "T00:00:00Z";
            formattedEndTime = todayMidnight.getYear() + "-" + todayMidnight.getMonthValue() + "-" + todayMidnight.getDayOfMonth() + "T00:00:00Z";
        }

        URL url = new URL("https://api.twitch.tv/helix/clips?game_id=" + Game.FORTNITE.getId() + "&first=25&started_at=" + formattedStartTime + "&ended_at=" + formattedEndTime);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.addRequestProperty("Client-ID", "");

        File out = File.createTempFile(UUID.randomUUID().toString(), ".json");
        System.out.println("Downloaded clips data at " + out.getPath());
        writeFile(out, connection.getInputStream());

        Gson gson = new Gson();
        return gson.fromJson(new FileReader(out), new TypeToken<List<TwitchClip>>() {
        }.getType());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

如果要清除所有缓存,请尝试:

{"data":[{"id":"HeadstrongObservantEagleANELE","url":"https://clips.twitch.tv/HeadstrongObservantEagleANELE","embed_url":"https://clips.twitch.tv/embed?clip=HeadstrongObservantEagleANELE","broadcaster_id":"81687332","broadcaster_name":"cloakzy","creator_id":"158774890","creator_name":"xvibes","video_id":"442904759","game_id":"33214","language":"en","title":"creatorcode: cloakzy !mouse","view_count":11755,"created_at":"2019-06-23T01:05:51Z","thumbnail_url":"https://clips-media-assets2.twitch.tv/34634505760-offset-684-preview-480x272.jpg"},{"id":"LongSpineyYakinikuPogChamp","url":"https://clips.twitch.tv/LongSpineyYakinikuPogChamp","embed_url":"https://clips.twitch.tv/embed?clip=LongSpineyYakinikuPogChamp","broadcaster_id":"81687332","broadcaster_name":"cloakzy","creator_id":"57403631","creator_name":"4rchr","video_id":"442904759","game_id":"33214","language":"en","title":"x","view_count":10204,"created_at":"2019-06-23T01:08:48Z","thumbnail_url":"https://clips-media-assets2.twitch.tv/34634505760-offset-860-preview-480x272.jpg"},{"id":"SuspiciousMistyTortoiseImGlitch","url":"https://clips.twitch.tv/SuspiciousMistyTortoiseImGlitch","embed_url":"https://clips.twitch.tv/embed?clip=SuspiciousMistyTortoiseImGlitch","broadcaster_id":"81687332","broadcaster_name":"cloakzy","creator_id":"139090954","creator_name":"Malgre","video_id":"442904759","game_id":"33214","language":"en","title":"cloak","view_count":8104,"created_at":"2019-06-23T01:12:36Z","thumbnail_url":"https://clips-media-assets2.twitch.tv/AT-cm%7C481623363-preview-480x272.jpg"}],"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7IkN1cnNvciI6Ik13PT0ifX0"}}

以下服务实现Exception in thread "Timer-0" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ at com.google.gson.Gson.fromJson(Gson.java:939) at com.google.gson.Gson.fromJson(Gson.java:892) at video.TwitchVideo.getVideoList(TwitchVideo.java:104) at video.TwitchVideo.<init>(TwitchVideo.java:19) at main.TwitchApplication$1.run(TwitchApplication.java:56) at java.util.TimerThread.mainLoop(Unknown Source) at java.util.TimerThread.run(Unknown Source) Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) at com.google.gson.Gson.fromJson(Gson.java:927) ... 6 more 并具有\Drupal::service('cache.render')->invalidateAll() 方法,该方法将所有缓存项标记为无效:

drupal_flush_all_caches()

答案 3 :(得分:0)

请检查该课程。

\Drush\Commands\core\CacheCommands

例如,您可以使用

CacheCommands::clearCssJs();

答案 4 :(得分:0)

以编程方式使用 PHP 只需在要清除缓存的地方使用此方法:

drupal_flush_all_caches();

还有其他清除缓存的方法:

通过管理界面

导航到 /admin/config/development/performance 并单击“清除所有缓存”按钮。

Drush

drush cache-rebuild
//or you can use
drush cr

通过 update.php 运行 update.php (/update.php) 是另一种清除缓存的方法。

通过 SQL 像这样删除以“cache_”开头的表中的所有数据:

TRUNCATE cache_config;
TRUNCATE cache_container;
TRUNCATE cache_data;
TRUNCATE cache_default;
TRUNCATE cache_discovery;
TRUNCATE cache_dynamic_page_cache;
TRUNCATE cache_entity;
TRUNCATE cache_menu;
TRUNCATE cache_render;
TRUNCATE cache_toolbar;

其他方法:

$variables['#cache']['max-age'] = 0;

\Drupal::service('page_cache_kill_switch')->trigger();

cache_clear_all() // For Drupal-7

drupal_flush_all_caches() // For Drupal-8

如果要清除渲染缓存等特定缓存,则可以运行以下代码:

\Drupal::service('cache.render')->invalidateAll();

如果你想清除一个特定的缓存,比如路由缓存,那么你可以运行以下代码:

\Drupal::service("router.builder")->rebuild();