Anchor does not work for global cache

I tried to use an anchor for a global cache definition but got the following error:
This GitLab CI configuration is invalid: jobs:build:cache config contains unknown keys: <<

Why does anchor not work in this case?

    cache: &global_cache
    - key: "$CI_COMMIT_REF_NAME"
      policy: pull
        - build
    - key:
          - gradle/wrapper/
      policy: pull          
        - .gradle/wrapper
        - .gradle/notifications   
    <<: *global_cache
    policy: push
  stage: build
  script: ./gradlew --build-cache  assemble


Because it’s not supported.

I ran into this same error today–anchors are advised for use in this exact scenario in the docs: Caching in GitLab CI/CD | GitLab

Can the docs be updated to advise a different course of action if this is unsupported? Thanks.

issue in OP’s post is not in the anchors, it is in the way how he defined cache. He has multiple key in cache in default, but none of that in the job spec. The example in docs is for a single key and is correct.