# Class: Cache

## Phaser. Cache

A game only has one instance of a Cache and it is used to store all externally loaded assets such as images, sounds and data files as a result of Loader calls. Cached items use string based keys for look-up.

#### new Cache(game)

  Phaser.Cache constructor.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `game` | Phaser.Game | A reference to the currently running game.

  Source:
    - loader/Cache.js, line 15

### Members

#### <static, constant> BINARY :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 161

#### <static, constant> BITMAPDATA :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 167

#### <static, constant> BITMAPFONT :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 173

#### <static, constant> CANVAS :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 119

#### <static, constant> IMAGE :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 125

#### <static, constant> JSON :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 179

#### <static, constant> PHYSICS :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 149

#### <static, constant> SOUND :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 137

#### <static, constant> TEXT :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 143

#### <static, constant> TEXTURE :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 131

#### <static, constant> TILEMAP :number

  ##### Type:

  - number
  Source:
    - loader/Cache.js, line 155

#### _cacheMap

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `_cacheMap` | array | Const to cache object look-up array.

  Source:
    - loader/Cache.js, line 99

#### game

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `game` | Phaser.Game | Local reference to game.

  Source:
    - loader/Cache.js, line 20

#### onSoundUnlock

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onSoundUnlock` | Phaser.Signal | This event is dispatched when the sound system is unlocked via a touch event on cellular devices.

  Source:
    - loader/Cache.js, line 94

### Methods

#### addBinary(key, binaryData)

  Add a binary object in to the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for this binary data.
  `binaryData` | object | The binary object to be addded to the cache.

  Source:
    - loader/Cache.js, line 197

#### addBitmapData(key, bitmapData) → {Phaser.BitmapData}

  Add a BitmapData object in to the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for this BitmapData.
  `bitmapData` | Phaser.BitmapData | The BitmapData object to be addded to the cache.

  Source:
    - loader/Cache.js, line 210

  ##### Returns:

  The BitmapData object to be addded to the cache.

  Type
    Phaser.BitmapData

#### addBitmapFont(key, url, data, xmlData, xSpacing, ySpacing)

  Add a new Bitmap Font to the Cache.

  ##### Parameters:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `key` | string |  |  | The unique key by which you will reference this object.
  `url` | string |  |  | URL of this font xml file.
  `data` | object |  |  | Extra font data.
  `xmlData` | object |  |  | Texture atlas frames data.
  `xSpacing` | number | <optional> | 0 | If you'd like to add additional horizontal spacing between the characters then set the pixel value here.
  `ySpacing` | number | <optional> | 0 | If you'd like to add additional vertical spacing between the lines then set the pixel value here.

  Source:
    - loader/Cache.js, line 312

#### addCanvas(key, canvas, context)

  Add a new canvas object in to the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for this canvas.
  `canvas` | HTMLCanvasElement | Canvas DOM element.
  `context` | CanvasRenderingContext2D | Render context of this canvas.

  Source:
    - loader/Cache.js, line 183

#### <protected> addDefaultImage()

  Adds a default image to be used in special cases such as WebGL Filters. Is mapped to the key __default.

  Source:
    - loader/Cache.js, line 349

#### addImage(key, url, data)

  Add a new image.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | The unique key by which you will reference this object.
  `url` | string | URL of this image file.
  `data` | object | Extra image data.

  Source:
    - loader/Cache.js, line 415

#### addJSON(key, url, data)

  Add a new json object into the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the text data.
  `url` | string | URL of this text data file.
  `data` | object | Extra text data.

  Source:
    - loader/Cache.js, line 401

#### <protected> addMissingImage()

  Adds an image to be used when a key is wrong / missing. Is mapped to the key __missing.

  Source:
    - loader/Cache.js, line 368

#### addRenderTexture(key, texture)

  Add a new Phaser.RenderTexture in to the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | The unique key by which you will reference this object.
  `texture` | Phaser.Texture | The texture to use as the base of the RenderTexture.

  Source:
    - loader/Cache.js, line 226

#### addSound(key, url, data, webAudio, audioTag)

  Add a new sound.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the sound.
  `url` | string | URL of this sound file.
  `data` | object | Extra sound data.
  `webAudio` | boolean | True if the file is using web audio.
  `audioTag` | boolean | True if the file is using legacy HTML audio.

  Source:
    - loader/Cache.js, line 434

#### addSpriteSheet(key, url, data, frameWidth, frameHeight, frameMax, margin, spacing)

  Add a new sprite sheet in to the cache.

  ##### Parameters:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `key` | string |  |  | The unique key by which you will reference this object.
  `url` | string |  |  | URL of this sprite sheet file.
  `data` | object |  |  | Extra sprite sheet data.
  `frameWidth` | number |  |  | Width of the sprite sheet.
  `frameHeight` | number |  |  | Height of the sprite sheet.
  `frameMax` | number | <optional> | -1 | How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.
  `margin` | number | <optional> | 0 | If the frames have been drawn with a margin, specify the amount here.
  `spacing` | number | <optional> | 0 | If the frames have been drawn with spacing between them, specify the amount here.

  Source:
    - loader/Cache.js, line 241

#### addText(key, url, data)

  Add a new text data.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the text data.
  `url` | string | URL of this text data file.
  `data` | object | Extra text data.

  Source:
    - loader/Cache.js, line 387

#### addTextureAtlas(key, url, data, atlasData, format)

  Add a new texture atlas to the Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | The unique key by which you will reference this object.
  `url` | string | URL of this texture atlas file.
  `data` | object | Extra texture atlas data.
  `atlasData` | object | Texture atlas frames data.
  `format` | number | The format of the texture atlas.

  Source:
    - loader/Cache.js, line 280

#### addTilemap(key, url, mapData, format)

  Add a new tilemap to the Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | The unique key by which you will reference this object.
  `url` | string | URL of the tilemap image.
  `mapData` | object | The tilemap data object (either a CSV or JSON file).
  `format` | number | The format of the tilemap data.

  Source:
    - loader/Cache.js, line 265

#### addTilemap(key, url, JSONData, format)

  Add a new physics data object to the Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | The unique key by which you will reference this object.
  `url` | string | URL of the physics json data.
  `JSONData` | object | The physics data object (a JSON file).
  `format` | number | The format of the physics data.

  Source:
    - loader/Cache.js, line 334

#### checkBinaryKey(key) → {boolean}

  Checks if the given key exists in the Binary Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 761

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkBitmapDataKey(key) → {boolean}

  Checks if the given key exists in the BitmapData Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 774

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkBitmapFontKey(key) → {boolean}

  Checks if the given key exists in the BitmapFont Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 787

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkCanvasKey(key) → {boolean}

  Checks if the given key exists in the Canvas Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 670

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkImageKey(key) → {boolean}

  Checks if the given key exists in the Image Cache. Note that this also includes Texture Atlases, Sprite Sheets and Retro Fonts.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 683

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkJSONKey(key) → {boolean}

  Checks if the given key exists in the JSON Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 800

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkKey(type, key) → {boolean}

  Checks if a key for the given cache object type exists.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `type` | number | The Cache type to check against. I.e. Phaser.Cache.CANVAS, Phaser.Cache.IMAGE, Phaser.Cache.JSON, etc.
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 651

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkPhysicsKey(key) → {boolean}

  Checks if the given key exists in the Physics Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 735

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkSoundKey(key) → {boolean}

  Checks if the given key exists in the Sound Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 709

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkTextKey(key) → {boolean}

  Checks if the given key exists in the Text Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 722

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkTextureKey(key) → {boolean}

  Checks if the given key exists in the Texture Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 696

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### checkTilemapKey(key) → {boolean}

  Checks if the given key exists in the Tilemap Cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to check is in the Cache.

  Source:
    - loader/Cache.js, line 748

  ##### Returns:

  True if the key exists, otherwise false.

  Type
    boolean

#### decodedSound(key, data)

  Add a new decoded sound.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the sound.
  `data` | object | Extra sound data.

  Source:
    - loader/Cache.js, line 513

#### destroy()

  Clears the cache. Removes every local cache object reference.

  Source:
    - loader/Cache.js, line 1299

#### getBinary(key) → {object}

  Get binary data by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the binary data object to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 1102

  ##### Returns:

  The binary data object.

  Type
    object

#### getBitmapData(key) → {Phaser.BitmapData}

  Get a BitmapData object from the cache by its key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the BitmapData object to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 548

  ##### Returns:

  The requested BitmapData object if found, or null if not.

  Type
    Phaser.BitmapData

#### getBitmapFont(key) → {Phaser.BitmapFont}

  Get a BitmapFont object from the cache by its key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the BitmapFont object to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 568

  ##### Returns:

  The requested BitmapFont object if found, or null if not.

  Type
    Phaser.BitmapFont

#### getCanvas(key) → {object}

  Get a canvas object from the cache by its key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the canvas to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 528

  ##### Returns:

  The canvas object.

  Type
    object

#### getFrame(key) → {Phaser.Frame}

  Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the frame data to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 921

  ##### Returns:

  The frame data.

  Type
    Phaser.Frame

#### getFrameByIndex(key) → {Phaser.Frame}

  Get a single frame out of a frameData set by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the frame data to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 887

  ##### Returns:

  The frame object.

  Type
    Phaser.Frame

#### getFrameByName(key) → {Phaser.Frame}

  Get a single frame out of a frameData set by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the frame data to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 904

  ##### Returns:

  The frame object.

  Type
    Phaser.Frame

#### getFrameData(key) → {Phaser.FrameData}

  Get frame data by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the frame data to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 853

  ##### Returns:

  The frame data.

  Type
    Phaser.FrameData

#### getImage(key) → {object}

  Get image data by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the image to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 813

  ##### Returns:

  The image data.

  Type
    object

#### getJSON(key) → {object}

  Get a JSON object by key from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the json object to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 1082

  ##### Returns:

  The JSON object.

  Type
    object

#### getKeys(type) → {Array}

  Gets all keys used by the Cache for the given data type.

  ##### Parameters:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `type` | number | <optional> | Phaser.Cache.IMAGE | The type of Cache keys you wish to get. Can be Cache.CANVAS, Cache.IMAGE, Cache.SOUND, etc.

  Source:
    - loader/Cache.js, line 1122

  ##### Returns:

  The array of item keys.

  Type
    Array

#### getPhysicsData(key, object, fixtureKey) → {object}

  Get a physics data object from the cache by its key. You can get either the entire data set, a single object or a single fixture of an object from it.

  ##### Parameters:

  Name | Type | Argument | Default | Description
  --- | --- | --- | --- | ---
  `key` | string |  |  | Asset key of the physics data object to retrieve from the Cache.
  `object` | string | <optional> | null | If specified it will return just the physics object that is part of the given key, if null it will return them all.
  `fixtureKey` | string |  |  | Fixture key of fixture inside an object. This key can be set per fixture with the Phaser Exporter.

  Source:
    - loader/Cache.js, line 588

  ##### Returns:

  The requested physics object data if found.

  Type
    object

#### getSound(key) → {Phaser.Sound}

  Get sound by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the sound to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 975

  ##### Returns:

  The sound object.

  Type
    Phaser.Sound

#### getSoundData(key) → {object}

  Get sound data by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the sound to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 995

  ##### Returns:

  The sound data.

  Type
    object

#### getText(key) → {object}

  Get text data by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the text data to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 1062

  ##### Returns:

  The text data.

  Type
    object

#### getTexture(key) → {Phaser.RenderTexture}

  Get a RenderTexture by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the RenderTexture to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 955

  ##### Returns:

  The RenderTexture object.

  Type
    Phaser.RenderTexture

#### getTextureFrame(key) → {Phaser.Frame}

  Get a single texture frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the frame to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 938

  ##### Returns:

  The frame data.

  Type
    Phaser.Frame

#### getTilemap(key) → {Object}

  Get tilemap data by key.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the tilemap data to retrieve from the Cache.

  Source:
    - loader/Cache.js, line 833

  ##### Returns:

  The raw tilemap data in CSV or JSON format.

  Type
    Object

#### isSoundDecoded(key) → {boolean}

  Check if the given sound has finished decoding.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the sound in the Cache.

  Source:
    - loader/Cache.js, line 1015

  ##### Returns:

  The decoded state of the Sound object.

  Type
    boolean

#### isSoundReady(key) → {boolean}

  Check if the given sound is ready for playback. A sound is considered ready when it has finished decoding and the device is no longer touch locked.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the sound in the Cache.

  Source:
    - loader/Cache.js, line 1031

  ##### Returns:

  True if the sound is decoded and the device is not touch locked.

  Type
    boolean

#### isSpriteSheet(key) → {boolean}

  Check whether an image asset is sprite sheet or not.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key of the sprite sheet you want.

  Source:
    - loader/Cache.js, line 1044

  ##### Returns:

  True if the image is a sprite sheet.

  Type
    boolean

#### reloadSound(key)

  Reload a sound.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the sound.

  Source:
    - loader/Cache.js, line 460

#### reloadSoundComplete(key)

  Fires the onSoundUnlock event when the sound has completed reloading.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the sound.

  Source:
    - loader/Cache.js, line 482

#### removeBinary(key)

  Removes a binary file from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1269

#### removeBitmapData(key)

  Removes a bitmap data from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1279

#### removeBitmapFont(key)

  Removes a bitmap font from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1289

#### removeCanvas(key)

  Removes a canvas from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1199

#### removeImage(key)

  Removes an image from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1209

#### removeJSON(key)

  Removes a json object from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1239

#### removePhysics(key)

  Removes a physics data file from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1249

#### removeSound(key)

  Removes a sound from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1219

#### removeText(key)

  Removes a text from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1229

#### removeTilemap(key)

  Removes a tilemap from the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Key of the asset you want to remove.

  Source:
    - loader/Cache.js, line 1259

#### updateFrameData(key, frameData)

  Replaces a set of frameData with a new Phaser.FrameData object.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | The unique key by which you will reference this object.
  `frameData` | number | The new FrameData.

  Source:
    - loader/Cache.js, line 870

#### updateSound(key)

  Updates the sound object in the cache.

  ##### Parameters:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the sound.

  Source:
    - loader/Cache.js, line 498