It would be really handy to be able to easily run cache_get from drush. Just connecting to the database / cache backend isn't enough, as usually objects are serialized and need PHP to unserialize them. Attached is a patch that implements a cache-get command:

$ drush4 cache-get --help
Fetch a cached object and display it.

Examples:
 drush cache-get schema                    Display the data for the cache id "schema" from the "cache" bin.                     
 drush cache-get                           Display the data for the cache id "update_available_releases" from the "update" bin. 
 update_available_releases update                                                                                               


Arguments:
 cid                                       The id of the object to fetch.         
 bin                                       Optional. The cache bin to fetch from. 


Aliases: cg

The attache patch applies to both the 5.x and 4.x branches.

Comments

msonnabaum’s picture

Seems pretty useful to me.

My only note is that I'd rather see the object returned as JSON than use drush_print_r(). That way you could possibly do something with the object rather than just view it.

msonnabaum’s picture

Oh and also, if we're going to do this, we need more than just "get".

I'm thinking this patch should probably turn clear.cache.inc into cache.drush.inc where cache clearing and getting/setting is all together.

greg.1.anderson’s picture

Yes, I agree, but I think that in general, var_export is more readable than JSON. Maybe add in a flag so that the caller can select the output format?

As far as doing something with the object, if you return the object as the result of the command hook, then it will be available in $result['object'] when called via backend invoke (if there is a use case for remotely fetching cache values... :> ).

msonnabaum’s picture

Yeah, JSON probably isn't good for default output, especially considering php doesn't really have a good pretty print option for it.

I'd be cool with a JSON flag like I did for node-show: #1005480: Create a new project to develop Drush entity support.

And just having come off a project that made heavy use of a SOAP backend and caching all of the SOAP responses, I can say that there is definitely a use case for remotely fetching those objects.

deviantintegral’s picture

StatusFileSize
new1.99 KB

Agreed about adding more cache_* commands and unifying them in one file. Strangely enough, I'm also working on a project involving SOAP and caching, hence why I was wanting an easy way to query our local cache.

Here's a patch that simply adds --json as a flag.

deviantintegral’s picture

StatusFileSize
new11 KB

Here's a patch that expands the cache functionality.

  • bc45035 Move cache functions to cache.drush.inc. Add var_export and better JSON support.
  • 79188a1 Error handling for missing cid parameter.
  • 5fed500 Don't automatically prepend cache_ to cache bins.
  • 37e36d6 Add support for setting an object in the cache in JSON format or as a string.

I didn't add import support for var_export() output as PHP doesn't have a stdClass::__setState() method.

Current patch applies identically to 4.x and 5.x.

moshe weitzman’s picture

Looks great.

  1. Lets not duplicate the --format option for each possible value. Lets consolidate to one option and document all supported values in the description. This will be more important when we start validating options during command execution.
  2. I think these commands could use 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE in the command definition. They would be much faster this way. The cache system is fully loaded by this time as far as I can tell. I checked D6 and D7. We'd have to go through the command callbacks and make sure that all code that expects a full bootstrap is changed or loads whats needed (e.g. drupal_json_encode(), drupal_to_js()).
moshe weitzman’s picture

Status: Needs review » Needs work

Hope someone can finish this off. It is close.

kotnik’s picture

StatusFileSize
new11.61 KB

Here's the patch that applies to HEAD with fixes from #7.

kotnik’s picture

Assigned: Unassigned » kotnik
Status: Needs work » Needs review

Please review.

kotnik’s picture

StatusFileSize
new12.44 KB

Updated patch (I wrongly set lower bootstrap for cache-clear).

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. I read the patches in order and so composed the following review which is now deprecated because your second commit in the patch fixes the problem. I really think we should post a single squashed commit to the issue queue. What would be the patch authoring process for that?

The patch is RTBC IMO. Will leave it open for a bit in case more comments come along.

+++ b/commands/core/cache.drush.inc
@@ -0,0 +1,237 @@
+      drupal_clear_css_cache();

This function is in common.inc which is not included until FULL. I don't see how this can be working.

+++ b/commands/core/cache.drush.inc
@@ -0,0 +1,237 @@
+      $types = drush_cache_clear_types();

I'm similarly worried that this function won't return same results as FULL.

maybe cc command should stay as full.

deviantintegral’s picture

I can't seem to apply either of the previous patches. They both reference commish 3953f1fe51b381eff7c10f10249c3ea9f8582cc4, which isn't in my tree.

To generate a single patch, just do "git diff master".

msonnabaum’s picture

StatusFileSize
new11.54 KB

Try this one, which squashes the two commits into one.

kotnik’s picture

Devianintegral, try Msonnabaum's patch. Mine were created against Drush master branch.

deviantintegral’s picture

StatusFileSize
new10.89 KB

I could only apply #14 by using patch. It looks like it was generated with --no-prefix, which I doesn't seem to work well with applying.

The JSON functions don't work without a full bootstrap, at least on D6. I think we do have to stick with a full bootstrap for this. Here's a patch with just the bootstrap keys removed.

msonnabaum’s picture

Status: Reviewed & tested by the community » Needs work

I've just added two new functions in master: drush_json_encode and drush_json_decode.

I'd like to get rid of the environment specific logic around the json handling, so lets just use these instead.

msonnabaum’s picture

StatusFileSize
new10.65 KB

Here's a patch with the changes I suggested. I can't seem to get it to work cleanly using format-patch and am, so its just a regular git diff.

deviantintegral’s picture

Status: Needs work » Needs review
StatusFileSize
new10.7 KB

The patch in #18 was missing returns from the switch statement for cache get, so all formats were printed out instead of the selected one.

moshe weitzman’s picture

Assigned: kotnik » msonnabaum
Status: Needs review » Reviewed & tested by the community

This is probably ready. Will let Mark do the final look over and commits.

deviantintegral’s picture

StatusFileSize
new10.7 KB

Here's a reroll against master.

msonnabaum’s picture

Status: Reviewed & tested by the community » Fixed

Ok, this is looking good enough for me.

Committed to master and backported to 4.

deviantintegral’s picture

Status: Fixed » Needs work

Something is wrong with the commit, as it only shows removing clear.cache.inc, and none of the other modifications. 7.x-4.x looks fine.

msonnabaum’s picture

Status: Needs work » Fixed

Good call. Forgot to add the cache inc.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.