Dancer...:Cache(3pm)

πŸ’– Dancer::Route::Cache β€” the manual, but make it slay πŸ’–

okay bestie, so this is Dancer::Route::Cache, and it lives in the User Contributed Perl Documentation section of the manual (section 3pm, if you're keeping score). It's a route caching mechanism for Dancer, and honestly? it's giving speedrun energy. πŸƒβ™€οΈβœ¨


✨ NAME ✨

Dancer::Route::Cache β€” route caching mechanism for Dancer πŸŽ€


🌸 VERSION 🌸

version 1.3522


πŸ¦‹ SYNOPSIS πŸ¦‹

my $cache = Dancer::Route::Cache->new(
    path_limit => 300, # optional, defaults to 600 (routes to cache)
    size_limit => 5M,  # optional, defaults to 10M (10MB)
);

# storing a path
# /new/item/ is the path, $route is a compiled route
$cache->store_path( 'get', '/new/item/', $route );
my $cached_route = $cache->route_from_path('/new/item/');

πŸ’… DESCRIPTION πŸ’…

so here's the tea, bestie β˜• β€”

When Dancer first starts up, it has to compile a regexp list of all the routes. Then, on every single request, it goes over that compiled routes list and tries to compare the requested path to a route. every. single. time. πŸ™ˆ

and the major drawback?? Dancer has to go over the matching on every request, which β€” especially on CGI-based applications β€” can be very time consuming. it's giving "why are we like this". 😭

so the caching mechanism swoops in like the main character ✨ and lets you cache some requests to specific routes (but NOT specific results, ok? important!) and run those routes on a specific path. Which allows us to speed up Dancer quite a lot. ate. πŸ’…


πŸ‘‘ METHODS/SUBROUTINES πŸ‘‘

πŸ“ new(@args)

Creates a new route cache object. brand new, fresh out the box, iconic.

my $cache = Dancer::Route::Cache->new(
    path_limit => 100,   # only 100 paths will be cached
    size_limit => '30M', # max size for cache is 30MB
);

Please check the "ATTRIBUTES" section below to learn about the arguments for new() β€” it's literally right there, go look. πŸ‘€

🧚 route_from_path($path)

Fetches the route from the path in the cache. that's it, that's the whole vibe.

⭐ store_path( $method, $path => $route )

Stores the route in the cache according to the path and $method.

For developers: the reason we're using an object for this and not directly using the registry hash is because we need to enforce the limits. yeah, the object is doing the boundary-setting. respect. 🫢

🌈 parse_size($size)

Parses the size wanted to bytes. It can handle Kilobytes, Megabytes or Gigabytes.

NOTICE: handles bytes, not bits! ⚠️ do not confuse them, bestie, that's a whole different number.
my $bytes = $cache->parse_size('30M');

# doesn't need an existing object
$bytes = Dancer::Route::Cache->parse_size('300G'); # works this way too

look at that β€” it works without an existing object too. independent. self-sufficient. πŸ‘‘

πŸ’œ route_cache_size

Returns a rough calculation the size of the cache. This is used to enforce the size limit. rough numbers, but she's doing her best πŸ’–

πŸŽ€ route_cache_paths

Returns all the paths in the cache. This is used to enforce the path limit.

Please be careful if you use "mount" in Plack::Builder and some applications β€” routes are linked with applications and same path may be in some applications but with different handlers! 😭 this is the "it's complicated" part, ok? same path, different handlers, do not get got.

✨ ATTRIBUTES ✨

🌸 size_limit($limit)

Allows one to set a size limit of the cache.

Returns the limit (post-set). so you set it, and it hands it right back to you. reciprocity. πŸ’ž

$cache->size_limit('10K');      # sets limit
my $limit = $cache->size_limit; # gets limit

πŸ¦‹ path_limit($limit)

A path limit. That is, the amount of paths that whose routes will be cached. (yes, we said it exactly like that, no notes πŸ“)

Returns the limit (post-set).

$cache->path_limit('100');      # sets limit
my $limit = $cache->path_limit; # gets limit

🌟 AUTHOR 🌟

Dancer Core Developers β€” absolute legends, no cap. 🫑


This software is copyright (c) 2010 by Alexis Sukrieh.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. slay, open source, we love to see it. πŸ’–


and that's the whole page bestie!! this was perl v5.40.1, dated 2026-05-05, from the User Contributed Perl Documentation section β€” mwah, go cache some routes, ily, bye!! πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from