|
| 1 | +# Workspace Analyzer Caches |
| 2 | + |
| 3 | +The Workspace Analyzer Caches module provides simple and efficient caching mechanisms for storing and reusing workspace analysis data. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Overview](#overview) |
| 8 | +- [Cache Types](#cache-types) |
| 9 | +- [Usage Examples](#usage-examples) |
| 10 | +- [Best Practices](#best-practices) |
| 11 | +- [API Reference](#api-reference) |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +The caches module provides simple caching mechanisms for workspace analysis data to improve performance: |
| 16 | + |
| 17 | +- **Memory Cache**: Fast in-memory storage for small to medium datasets |
| 18 | +- **Disk Cache**: Persistent storage for large datasets or long-term use |
| 19 | +- **Simple API**: Easy-to-use interface for both cache types |
| 20 | + |
| 21 | +### When to Use |
| 22 | + |
| 23 | +- **Memory Cache**: When you have sufficient RAM and need fast access |
| 24 | +- **Disk Cache**: When working with large datasets or need to persist results between sessions |
| 25 | + |
| 26 | + |
| 27 | +## Cache Types |
| 28 | + |
| 29 | +### Memory Cache |
| 30 | + |
| 31 | +Fast in-memory caching for medium-sized datasets: |
| 32 | + |
| 33 | +```python |
| 34 | +from embodichain.lab.sim.utility.workspace_analyzer.caches import MemoryCache |
| 35 | + |
| 36 | +# Create memory cache |
| 37 | +cache = MemoryCache() |
| 38 | + |
| 39 | +# Add data |
| 40 | +positions = [...] # Your pose data |
| 41 | +cache.add(positions) |
| 42 | + |
| 43 | +# Get all data |
| 44 | +all_data = cache.get_all() |
| 45 | +``` |
| 46 | + |
| 47 | +### Disk Cache |
| 48 | + |
| 49 | +Persistent disk caching for large datasets: |
| 50 | + |
| 51 | +```python |
| 52 | +from embodichain.lab.sim.utility.workspace_analyzer.caches import DiskCache |
| 53 | + |
| 54 | +# Create disk cache |
| 55 | +cache = DiskCache(save_dir="./my_cache") |
| 56 | + |
| 57 | +# Add data |
| 58 | +positions = [...] # Your pose data |
| 59 | +cache.add(positions) |
| 60 | +cache.flush() # Save to disk |
| 61 | + |
| 62 | +# Get data |
| 63 | +all_data = cache.get_all() |
| 64 | +``` |
| 65 | + |
| 66 | +### Cache Manager |
| 67 | + |
| 68 | +Simplified factory pattern for creating caches: |
| 69 | + |
| 70 | +```python |
| 71 | +from embodichain.lab.sim.utility.workspace_analyzer.caches import CacheManager |
| 72 | + |
| 73 | +# Create memory cache |
| 74 | +memory_cache = CacheManager.create_cache("memory") |
| 75 | + |
| 76 | +# Create disk cache |
| 77 | +disk_cache = CacheManager.create_cache("disk", save_dir="./cache") |
| 78 | +``` |
| 79 | + |
| 80 | +## Usage Examples |
| 81 | + |
| 82 | +### Basic Usage |
| 83 | + |
| 84 | +```python |
| 85 | +import numpy as np |
| 86 | +from embodichain.lab.sim.utility.workspace_analyzer.caches import MemoryCache, DiskCache |
| 87 | + |
| 88 | +# Memory cache example |
| 89 | +memory_cache = MemoryCache() |
| 90 | + |
| 91 | +# Add pose data |
| 92 | +poses = [np.eye(4) for _ in range(1000)] # Example data |
| 93 | +memory_cache.add(poses) |
| 94 | + |
| 95 | +# Get data |
| 96 | +cached_poses = memory_cache.get_all() |
| 97 | +print(f"Cached {len(cached_poses)} poses") |
| 98 | + |
| 99 | +# Disk cache example |
| 100 | +disk_cache = DiskCache(save_dir="./workspace_cache") |
| 101 | +disk_cache.add(poses) |
| 102 | +disk_cache.flush() # Save to disk |
| 103 | + |
| 104 | +# Reload data |
| 105 | +reloaded_poses = disk_cache.get_all() |
| 106 | +``` |
| 107 | + |
| 108 | +### Real-world Usage |
| 109 | + |
| 110 | +```python |
| 111 | +# Cache usage in workspace analysis |
| 112 | +def analyze_workspace_with_cache(robot_config, use_cache=True): |
| 113 | + if use_cache: |
| 114 | + # Use disk cache to save results |
| 115 | + cache = DiskCache(save_dir=f"./cache_{robot_config.name}") |
| 116 | + |
| 117 | + # Check if cached data exists |
| 118 | + if cache.get_batch_count() > 0: |
| 119 | + print("Loading data from cache...") |
| 120 | + return cache.get_all() |
| 121 | + |
| 122 | + # Generate new workspace data |
| 123 | + print("Generating new workspace data...") |
| 124 | + poses = generate_workspace_poses(robot_config) |
| 125 | + |
| 126 | + if use_cache: |
| 127 | + # Save results |
| 128 | + cache.add(poses) |
| 129 | + cache.flush() |
| 130 | + print(f"Cached {len(poses)} poses to disk") |
| 131 | + |
| 132 | + return poses |
| 133 | +``` |
| 134 | + |
| 135 | +## Best Practices |
| 136 | + |
| 137 | +### Choosing Cache Type |
| 138 | + |
| 139 | +- **Small datasets (< 100k poses)**: Use `MemoryCache` |
| 140 | +- **Large datasets (> 100k poses)**: Use `DiskCache` |
| 141 | +- **Need persistence**: Use `DiskCache` |
| 142 | +- **Temporary computation**: Use `MemoryCache` |
| 143 | + |
| 144 | +### Simple Selection Function |
| 145 | + |
| 146 | +```python |
| 147 | +def choose_cache(data_size, need_persistence=False): |
| 148 | + """Choose cache type based on data size and requirements""" |
| 149 | + if need_persistence or data_size > 100000: |
| 150 | + return DiskCache() |
| 151 | + else: |
| 152 | + return MemoryCache() |
| 153 | + |
| 154 | +# Usage example |
| 155 | +cache = choose_cache(data_size=50000, need_persistence=True) |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +## API Reference |
| 161 | + |
| 162 | +### Basic Methods |
| 163 | + |
| 164 | +All cache classes support these basic operations: |
| 165 | + |
| 166 | +```python |
| 167 | +# Add data |
| 168 | +cache.add(poses_list) |
| 169 | + |
| 170 | +# Get all data |
| 171 | +all_data = cache.get_all() |
| 172 | + |
| 173 | +# Clear cache |
| 174 | +cache.clear() |
| 175 | + |
| 176 | +# Flush to disk (DiskCache only) |
| 177 | +cache.flush() |
| 178 | +``` |
| 179 | + |
| 180 | +### Creating Caches |
| 181 | + |
| 182 | +```python |
| 183 | +# Memory cache |
| 184 | +memory_cache = MemoryCache() |
| 185 | + |
| 186 | +# Disk cache |
| 187 | +disk_cache = DiskCache(save_dir="./my_cache") |
| 188 | + |
| 189 | +# Using factory method |
| 190 | +cache = CacheManager.create_cache("memory") # or "disk" |
| 191 | +``` |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +## Quick Start |
| 196 | + |
| 197 | +```python |
| 198 | +# Simplest usage |
| 199 | +from embodichain.lab.sim.utility.workspace_analyzer.caches import MemoryCache, DiskCache |
| 200 | + |
| 201 | +# For small datasets |
| 202 | +cache = MemoryCache() |
| 203 | + |
| 204 | +# For large datasets or persistence needed |
| 205 | +cache = DiskCache() |
| 206 | + |
| 207 | +# Add data |
| 208 | +cache.add(your_poses) |
| 209 | + |
| 210 | +# Get data |
| 211 | +result = cache.get_all() |
| 212 | +``` |
0 commit comments