|
| 1 | +--- |
| 2 | +title: CacheManagement |
| 3 | +tags: [Cache, Cache Management, Offline Manager, Offline Packs, Validate Cache, Invalidate Cache, Get Cache Size, Set Max Cache Size] |
| 4 | +custom_props: |
| 5 | + example_rel_path: CacheManagement/CacheManagement.tsx |
| 6 | +custom_edit_url: https://github.com/rnmapbox/maps/tree/master/example/src/examples/CacheManagement/CacheManagement.tsx |
| 7 | +--- |
| 8 | + |
| 9 | +Manages map cache. |
| 10 | + |
| 11 | +Uses the offline manager to manage the cache and the local storage in general. Shows how to invalidate cache to remove outdated tiles, how to clear the entire local storage from tiles and offline packs and to visualize the local storage usage amount. |
| 12 | + |
| 13 | + |
| 14 | +```jsx |
| 15 | +import React, { useState, useCallback } from 'react'; |
| 16 | +import MapboxGL, { MapView, Camera } from '@rnmapbox/maps'; |
| 17 | +import { |
| 18 | + Alert, |
| 19 | + StyleSheet, |
| 20 | + View, |
| 21 | + TextInput, |
| 22 | + TouchableOpacity, |
| 23 | + Text, |
| 24 | +} from 'react-native'; |
| 25 | + |
| 26 | +import sheet from '../../styles/sheet'; |
| 27 | +import { DEFAULT_CENTER_COORDINATE } from '../../utils'; |
| 28 | +import BaseExamplePropTypes, { |
| 29 | + BaseExampleProps, |
| 30 | +} from '../common/BaseExamplePropTypes'; |
| 31 | +import { ExampleWithMetadata } from '../common/ExampleMetadata'; |
| 32 | + |
| 33 | +const styles = StyleSheet.create({ |
| 34 | + button: { |
| 35 | + alignItems: 'center', |
| 36 | + backgroundColor: 'blue', |
| 37 | + borderRadius: 3, |
| 38 | + justifyContent: 'center', |
| 39 | + padding: 8, |
| 40 | + width: '100%', |
| 41 | + }, |
| 42 | + buttonTxt: { |
| 43 | + color: 'white', |
| 44 | + textAlign: 'center', |
| 45 | + }, |
| 46 | + control: { |
| 47 | + alignItems: 'center', |
| 48 | + justifyContent: 'center', |
| 49 | + margin: 16, |
| 50 | + padding: 8, |
| 51 | + width: '40%', |
| 52 | + }, |
| 53 | + controlsContainer: { |
| 54 | + flexDirection: 'row', |
| 55 | + flexWrap: 'wrap', |
| 56 | + }, |
| 57 | + textInput: { |
| 58 | + borderBottomColor: 'grey', |
| 59 | + borderBottomWidth: 1, |
| 60 | + marginBottom: 8, |
| 61 | + padding: 8, |
| 62 | + width: '100%', |
| 63 | + }, |
| 64 | +}); |
| 65 | + |
| 66 | +const CacheManagement: React.FC<Partial<BaseExampleProps>> = () => { |
| 67 | + const [cacheSize, setCacheSize] = useState(''); |
| 68 | + |
| 69 | + const invalidateAmbientCache = useCallback(async () => { |
| 70 | + await MapboxGL.offlineManager.invalidateAmbientCache(); |
| 71 | + Alert.alert('Ambient cache successfully invalidated'); |
| 72 | + }, []); |
| 73 | + |
| 74 | + const resetDatabase = useCallback(async () => { |
| 75 | + await MapboxGL.offlineManager.resetDatabase(); |
| 76 | + Alert.alert('Database successfully reset'); |
| 77 | + }, []); |
| 78 | + |
| 79 | + const clearAmbientCache = useCallback(async () => { |
| 80 | + await MapboxGL.offlineManager.clearAmbientCache(); |
| 81 | + Alert.alert('Ambient cache successfully cleared'); |
| 82 | + }, []); |
| 83 | + |
| 84 | + const setMaximumAmbientCacheSize = useCallback(async () => { |
| 85 | + const newMaxSize = parseInt(cacheSize, 10); |
| 86 | + await MapboxGL.offlineManager.setMaximumAmbientCacheSize(newMaxSize); |
| 87 | + Alert.alert(`Max cache size successfully set to ${newMaxSize} bytes`); |
| 88 | + }, [cacheSize]); |
| 89 | + |
| 90 | + const validateCacheInputValue = useCallback( |
| 91 | + (value: string) => !isNaN(parseInt(value, 10)), |
| 92 | + [], |
| 93 | + ); |
| 94 | + |
| 95 | + const onChangeCacheSize = useCallback( |
| 96 | + (value: string) => setCacheSize(value), |
| 97 | + [], |
| 98 | + ); |
| 99 | + |
| 100 | + const validSizeValue = validateCacheInputValue(cacheSize); |
| 101 | + const buttonStyles = validSizeValue |
| 102 | + ? styles.button |
| 103 | + : [styles.button, { backgroundColor: 'grey' }]; |
| 104 | + |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <MapView style={sheet.matchParent}> |
| 108 | + <Camera zoomLevel={16} centerCoordinate={DEFAULT_CENTER_COORDINATE} /> |
| 109 | + </MapView> |
| 110 | + |
| 111 | + <View style={styles.controlsContainer}> |
| 112 | + <View style={styles.control}> |
| 113 | + <TouchableOpacity |
| 114 | + onPress={invalidateAmbientCache} |
| 115 | + style={styles.button} |
| 116 | + > |
| 117 | + <Text style={styles.buttonTxt}>Invalidate cache</Text> |
| 118 | + </TouchableOpacity> |
| 119 | + </View> |
| 120 | + |
| 121 | + <View style={styles.control}> |
| 122 | + <TouchableOpacity onPress={resetDatabase} style={styles.button}> |
| 123 | + <Text style={styles.buttonTxt}>Reset database</Text> |
| 124 | + </TouchableOpacity> |
| 125 | + </View> |
| 126 | + |
| 127 | + <View style={styles.control}> |
| 128 | + <TextInput |
| 129 | + onChangeText={onChangeCacheSize} |
| 130 | + value={cacheSize} |
| 131 | + placeholder="New max" |
| 132 | + keyboardType="numeric" |
| 133 | + style={styles.textInput} |
| 134 | + /> |
| 135 | + <TouchableOpacity |
| 136 | + onPress={setMaximumAmbientCacheSize} |
| 137 | + style={buttonStyles} |
| 138 | + disabled={!validSizeValue} |
| 139 | + > |
| 140 | + <Text style={styles.buttonTxt}>Set ambient max cache</Text> |
| 141 | + </TouchableOpacity> |
| 142 | + </View> |
| 143 | + |
| 144 | + <View style={styles.control}> |
| 145 | + <TouchableOpacity onPress={clearAmbientCache} style={styles.button}> |
| 146 | + <Text style={styles.buttonTxt}>Clear ambient cache</Text> |
| 147 | + </TouchableOpacity> |
| 148 | + </View> |
| 149 | + </View> |
| 150 | + </> |
| 151 | + ); |
| 152 | +}; |
| 153 | + |
| 154 | +CacheManagement.propTypes = { |
| 155 | + ...BaseExamplePropTypes, |
| 156 | +}; |
| 157 | + |
| 158 | +export default CacheManagement; |
| 159 | + |
| 160 | + |
| 161 | +``` |
| 162 | + |
| 163 | +} |
| 164 | + |
0 commit comments