# Re-using cache key

**URL:** <https://discuss.circleci.com/t/re-using-cache-key/43000>\
**Category:** Build Environment\
**Created:** [February 18, 2022, 7:16pm UTC](https://discuss.circleci.com/t/re-using-cache-key/43000 "2022-02-18T19:16:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaufgang](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/jaufgang/32/29452_2.png) [@jaufgang](https://discuss.circleci.com/u/jaufgang)\
**Post date:** [February 18, 2022, 7:16pm UTC](https://discuss.circleci.com/t/re-using-cache-key/43000/1 "2022-02-18T19:16:40Z")

</div>

I have a workflow on a monorepo that has multiple package.json files. the cache key looks something like this:

```yaml
    - restore_cache:
          keys:
            - some-prefix-{{ checksum "package-lock.json" }}-{{
              checksum "apps/app1/package-lock.json" }}-{{
              checksum "apps/app2/functions/package-lock.json" }}

```

this cache key appears many times in my config.yml file.

I’m wondering if it’s possible to declare the cache key in one location and save it as a variable, and then referencing it in multiple locations. Something like this:

```yaml
    - restore_cache:
          keys:
            - << my-saved-cache-key >>

```

Is this possible, and what what would be the syntax? Any references do documentation for declaring a local variable like this would be helpful. Thanks!

---

<div class="post-metadata">

**Author:** ![rit1010](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/rit1010/32/34738_2.png) [@rit1010](https://discuss.circleci.com/u/rit1010)\
**Post date:** [February 19, 2022, 12:32am UTC](https://discuss.circleci.com/t/re-using-cache-key/43000/2 "2022-02-19T00:32:10Z")

</div>

There was an answer to using environment variables in config.yml that linked to the following example file.

```
 https://github.com/drevops/drevops/blob/8.x/.circleci/config.yml

```

This is an example of the use of aliases that may allow you to do what you want, more info is exchanged in this old thread

```
https://discuss.circleci.com/t/how-can-steps-in-yaml-config-be-reused/21876/4

```

and this blog post

[YAML - config.yml | CircleCI](https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/)

---

<div class="post-metadata">

**Author:** ![FelicianoTech](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/felicianotech/32/21348_2.png) [@FelicianoTech](https://discuss.circleci.com/u/FelicianoTech)\
**Post date:** [February 22, 2022, 4:18pm UTC](https://discuss.circleci.com/t/re-using-cache-key/43000/3 "2022-02-22T16:18:05Z")

</div>

It can’t be done exactly how you want, but there are many alternative ways.

1. via YAML, see @rit1010 post
2. via CircleCI reusable config: [Reusable Config Reference Guide - CircleCI](https://circleci.com/docs/2.0/reusing-config/#steps)
3. Via orbs, which is really #2 but in an orb
